#!/usr/bin/env python
# -*- coding:UTF-8 -*-
from django.http import HttpResponse, HttpResponseRedirect, StreamingHttpResponse, JsonResponse
from django.shortcuts import render, render_to_response
from django.template import RequestContext
from django import forms

import sys
import traceback
import logging
import cgi, cgitb
from os import environ
from string import strip, split
import datetime, time
from time import strftime,gmtime
import MySQLdb
import base64
import socket
from struct import *
import ctypes
import json
import hashlib
import os
import csv
import codecs
import web_XZDQ_V1_0.MySQLDB as MySQLDB
import web_XZDQ_V1_0.PowerID as PowerID
import web_XZDQ_V1_0.Common as Common
from django.views.decorators.csrf import csrf_exempt


# Create your views here.

# 装饰器html 权限验证 系统管理id=18
def auth(func):
    def inner(request):
        # 权限验证
        menudate = []
        try:
            userid = request.COOKIES['userid']
            row0 = MySQLDB.SelectMySql("SELECT role_id FROM t_sys_user WHERE user_id = %s AND status = 1;" % userid)
            # if row0[0][1] == 0:
            #    response = render(request,'login.html', {})
            #    return response
            row1 = MySQLDB.SelectMySql("SELECT menu_id FROM t_role_menu WHERE role_id = %s;" % row0[0][0])
            for i in range(len(row1)):
                menudate.append(row1[i][0])
        except:
            pass
        if 18 not in menudate:
            # return HttpResponseRedirect('/')
            response = render(request, 'login.html', {})
            return response
        # 多用户登录验证
        md5str = request.COOKIES['loginmd5']
        row0 = MySQLDB.SelectMySql("SELECT user_id FROM t_sys_user WHERE valid_md5 = '%s';" % md5str)
        try:
            if str(row0[0][0]) == userid:
                pass
            else:
                # return HttpResponseRedirect('/')
                response = render(request, 'login.html', {})
                return response
        except:
            # return HttpResponseRedirect('/')
            response = render(request, 'login.html', {})
            return response
        return func(request)

    return inner


# 装饰器json 权限验证 系统管理id=18
def authjson(func):
    def jnner(request):
        # 权限验证
        menudate = []
        try:
            userid = request.COOKIES['userid']
            row0 = MySQLDB.SelectMySql("SELECT role_id FROM t_sys_user WHERE user_id = %s AND status = 1;" % userid)
            row1 = MySQLDB.SelectMySql("SELECT menu_id FROM t_role_menu WHERE role_id = %s;" % row0[0][0])
            for i in range(len(row1)):
                menudate.append(row1[i][0])
        except:
            data = {
                "status": 0,
                "message": u"登录超时，请重新登陆!",
                "data": []
            }
            jsonstr = json.dumps(data)
            return HttpResponse(jsonstr, content_type="application/json")
        if 18 not in menudate:
            data = {
                "status": 0,
                "message": u"权限不够！",
                "data": []
            }
            jsonstr = json.dumps(data)
            return HttpResponse(jsonstr, content_type="application/json")
        # 多用户登录验证
        md5str = request.COOKIES['loginmd5']
        row0 = MySQLDB.SelectMySql("SELECT user_id FROM t_sys_user WHERE valid_md5 = '%s';" % md5str)
        try:
            if str(row0[0][0]) == userid:
                pass
            else:
                data = {
                    "status": 0,
                    "message": u"该账号在其他地方登录",
                    "data": []
                }
                jsonstr = json.dumps(data)
                return HttpResponse(jsonstr, content_type="application/json")
        except:
            data = {
                "status": 0,
                "message": u"其他错误",
                "data": []
            }
            jsonstr = json.dumps(data)
            return HttpResponse(jsonstr, content_type="application/json")
        return func(request)

    return jnner


# 数据字典首页 页面
@auth
def QLGL_ZDSJ_index(request):
    response = render(request, 'QLGL/ZDSJ/QLGL_ZDSJ_index.html', {})
    return response


# 功能：html界面跳转方法
# 输入参数：request
# 返回参数：responsehtml
# 参数管理桥梁参数首页 页面
@auth
def QLXJ_CSGL_QLCS(request):
    response = render(request, 'QLGL/ZDSJ/QLGL_ZDSJ_QLCS.html', {})
    return response


# 功能：html界面跳转方法
# 输入参数：request
# 返回参数：responsehtml
# 参数管理桥梁参数新增 页面
@auth
def QLXJ_QLCS_ADD(request):
    xparam_id = request.GET['param_id']
    param_id = int(xparam_id)
    # 新增下拉框列表
    try:
        row = MySQLDB.SelectMySql(
            "SELECT param_id, param_name FROM t_sys_param where status=1 and parent_id=0 order by orderby;")
        gsalist = []
        info = {}
        info["param_id"] = 0
        info["param_name"] = u'根节点'
        gsalist.append(info)
        if len(row) > 0:
            for i in range(len(row)):
                info = {}
                info["param_id"] = row[i][0]
                info["param_name"] = row[i][1]
                gsalist.append(info)
    except Exception as e:
        gsalist = []
    response = render(request, 'QLGL/ZDSJ/QLGL_ZDSJ_QLCS_ADD.html', {"gsalist": gsalist, "param_id": param_id})
    return response


# 功能：html界面跳转方法
# 输入参数：request
# 返回参数：responsehtml
# 参数管理桥梁参数修改 页面
@auth
def QLXJ_QLCS_MOD(request):
    xparam_id = request.GET['param_id']
    param_id = int(xparam_id)
    try:
        row = MySQLDB.SelectMySql(
            "SELECT param_id, param_name, param_code, parent_id, orderby FROM t_sys_param where status=1 and param_id='%s';" % (
            param_id))
        gsalist = []
        if len(row) > 0:
            for i in range(len(row)):
                info = {}
                info["param_id"] = row[i][0]
                info["param_name"] = row[i][1]
                info["param_code"] = row[i][2]
                info["parent_id"] = row[i][3]
                info["orderby"] = row[i][4]
                gsalist.append(info)
    except Exception as e:
        gsalist = []
    response = render(request, 'QLGL/ZDSJ/QLGL_ZDSJ_QLCS_MODIFY.html', {"gsalist": gsalist})
    return response


# 功能：html界面跳转方法
# 输入参数：request
# 返回参数：responsehtml
# 参数管理属性参数页面
@auth
def QLXJ_CSGL_SXCS(request):
    response = render(request, 'QLGL/ZDSJ/QLGL_ZDSJ_SXCS.html', {})
    return response


# 功能：html界面跳转方法
# 输入参数：request
# 返回参数：responsehtml
# 参数管理属性参数修改 页面
@auth
def QLXJ_SXCS_MOD(request):
    xid = request.GET['id']
    xid = int(xid)
    try:
        row = MySQLDB.SelectMySql(
            "SELECT id,name,param_type,orderby,remark from t_sys_attribute where status=1 and id= %s;" % (xid))
        gsalist = []
        if len(row) > 0:
            for i in range(len(row)):
                info = {}
                info["id"] = row[i][0]
                info["name"] = row[i][1]
                info["param_type"] = row[i][2]
                info["orderby"] = row[i][3]
                info["remark"] = row[i][4]
                gsalist.append(info)
    except Exception as e:
        gsalist = []
    response = render(request, 'QLGL/ZDSJ/QLGL_ZDSJ_SXCS_MODIFY.html', {"gsalist": gsalist})
    return response


# 参数管理参数参数新增 页面
@auth
def QLGL_ZDSJ_SXCS_ADD(request):
    response = render(request, 'QLGL/ZDSJ/QLGL_ZDSJ_SXCS_ADD.html', {})
    return response


##########病害标度html#######

# 病害标度主页面
@auth
def QLXJ_CSGL_BHBD(request):
    response = render(request, 'QLGL/ZDSJ/QLGL_ZDSJ_BHBD.html')
    return response


# 新增病害页面
@auth
def QLGL_ZDSJ_BHBD_DISEASE_ADD(request):
    response = render(request, 'QLGL/ZDSJ/QLGL_ZDSJ_BHBD_DISEASE_ADD.html')
    return response


# 新增标度 页面
@auth
def QLXJ_JCSJ_BHBD_addBD(request):
    # 新增标度病害下拉框列表
    disease_id = request.GET['disease_id']
    disease_id = int(disease_id)
    try:
        row = MySQLDB.SelectMySql("SELECT disease_id, disease_name FROM t_sys_disease where status=1 ORDER by orderby;")
        gsalist = []
        if len(row) > 0:
            for i in range(len(row)):
                info = {}
                info["disease_id"] = row[i][0]
                info["disease_name"] = row[i][1]
                gsalist.append(info)
    except Exception as e:
        gsalist = []
    response = render(request, 'QLGL/ZDSJ/QLGL_ZDSJ_BHBD_DISEASE_SALE_ADD.html',
                      {"gsalist": gsalist, "disease_id": disease_id})
    return response


# 修改病害 页面
@auth
def QLXJ_JCSJ_BHBD_MODBH(request):
    xid = request.GET['disease_id']
    disease_id = int(xid)
    # 新增标度下拉框列表
    try:
        row = MySQLDB.SelectMySql(
            "SELECT disease_id, disease_name, orderby FROM t_sys_disease where status=1 and disease_id='%s';" % (
            disease_id))
        gsalist = []
        if len(row) > 0:
            for i in range(len(row)):
                info = {}
                info["disease_id"] = row[i][0]
                info["disease_name"] = row[i][1]
                info["orderby"] = row[i][2]
                gsalist.append(info)
    except Exception as e:
        gsalist = []
    response = render(request, 'QLGL/ZDSJ/QLGL_ZDSJ_BHBD_DISEASE_MODIFY.html', {"gsalist": gsalist})
    return response


# 修改标度 页面
@auth
def QLXJ_JCSJ_BHBD_MODBD(request):
    xid = request.GET['disease_scale_id']
    disease_scale_id = int(xid)
    # 修改标度下拉框列表
    try:
        row = MySQLDB.SelectMySql(
            "SELECT disease_id,level,qualitative,quantitative,sumup,disease_scale_id FROM t_sys_disease_scale where disease_scale_id = %s;" % disease_scale_id)
        gsalist = []
        if len(row) > 0:
            for i in range(len(row)):
                info = {}
                info["disease_scale_id"] = disease_scale_id
                info["disease_id"] = row[i][0]
                info["level"] = row[i][1]
                info["qualitative"] = row[i][2]
                info["quantitative"] = row[i][3]
                info["sumup"] = row[i][4]
                bmrow = MySQLDB.SelectMySql("SELECT disease_id, disease_name FROM t_sys_disease where status=1;")
                bmdata = []
                try:
                    for j in range(len(bmrow)):
                        infodata = {}
                        infodata["disease_id"] = bmrow[j][0]
                        infodata["disease_name"] = bmrow[j][1]
                        bmdata.append(infodata)
                except:
                    pass
                info["BHdata"] = bmdata
                gsalist.append(info)
    except Exception as e:
        gsalist = []
    response = render(request, 'QLGL/ZDSJ/QLGL_ZDSJ_BHBD_DISEASE_SALE_MODIFY.html', {"gsalist": gsalist})
    return response


# 部件权重主页面
@auth
def QLXJ_BJQZ_index(request):
    response = render(request, 'QLGL/ZDSJ/QLGL_ZDSJ_BJQZ.html')
    return response


# 部件列表主页面
@auth
def QLXJ_BJLB_index(request):
    response = render(request, 'QLGL/ZDSJ/QLGL_ZDSJ_BJLB.html')
    return response


######################
# 参数管理方法
######################

# 功能：桥梁参数tree信息
# 输入参数：request
# 返回参数：response
@authjson
def QLXJ_QLCS_TREE(request):
    # 桥梁参数列表
    try:
        row = MySQLDB.SelectMySql(
            "SELECT param_id, param_name, param_code, parent_id, orderby FROM t_sys_param where status=1 and parent_id=0 order by orderby;")
        gsalist = []
        if len(row) > 0:
            for i in range(len(row)):
                info = {}
                info["param_id"] = row[i][0]
                info["param_name"] = row[i][1]
                info["param_code"] = row[i][2]
                info["parent_id"] = row[i][3]
                info["orderby"] = row[i][4]
                bmrow = MySQLDB.SelectMySql(
                    "SELECT param_id, param_name, param_code, parent_id, orderby FROM t_sys_param where status=1 and parent_id='%s' order by orderby;" %
                    row[i][0])
                bmdata = []
                try:
                    for j in range(len(bmrow)):
                        infodata = {}
                        infodata["param_id"] = bmrow[j][0]
                        infodata["param_name"] = bmrow[j][1]
                        infodata["param_code"] = bmrow[j][2]
                        infodata["parent_id"] = bmrow[j][3]
                        infodata["orderby"] = bmrow[j][4]
                        bmdata.append(infodata)
                except:
                    pass
                info["subdata"] = bmdata
                gsalist.append(info)
        data = {
            "status": 1,
            "message": u"查询桥梁参数tree信息成功",
            "data": gsalist
        }
    except Exception as e:
        data = {
            "status": 0,
            "message": u"查询桥梁参数tree信息失败",
            "data": []
        }
    jsonstr = json.dumps(data)
    return HttpResponse(jsonstr, content_type="application/json")


# 功能：新增桥梁参数信息
# 输入参数：request
# 返回参数：response
@authjson
def QLXJ_QLCS_ADDSAVE(request):
    param_name = request.POST['param_name']
    xparent_id = request.POST['parent_id']
    parent_id = int(xparent_id)
    xorderby = request.POST['orderby']
    orderby = int(xorderby)
    if parent_id == 0:
        try:
            param_code = request.POST['param_code']
            # 数据验证
            valid = MySQLDB.SelectMySql("select 1 from t_sys_param where param_code ='%s' and status = 1" % param_code)
            if len(valid) > 0:
                data = {
                    "status": 0,
                    "message": u"参数编码已存在，请重新输入",
                    "data": []
                }
            else:
                row = MySQLDB.CommitMySql(
                    "INSERT INTO t_sys_param (param_name,param_code,parent_id,status,orderby) VALUES ('%s','%s','%s',1,'%s');" % (
                    param_name, param_code, parent_id, orderby))
                data = {
                    "status": 1,
                    "message": u"新增参数配置成功",
                    "data": []
                }
        except Exception as e:
            data = {
                "status": 0,
                "message": u"新增参数配置失败",
                "data": []
            }
    else:
        try:
            row1 = MySQLDB.SelectMySql(
                "SELECT count(1) FROM t_sys_param where status=1 and parent_id='%s';" % (parent_id))
            row2 = MySQLDB.SelectMySql(
                "SELECT param_code FROM t_sys_param where status=1 and param_id='%s';" % (parent_id))
            param_code = row2[0][0] + '-' + str(row1[0][0])
            row3 = MySQLDB.CommitMySql(
                "INSERT INTO t_sys_param (param_name,param_code,parent_id,status,orderby) VALUES ('%s','%s','%s',1,'%s');" % (
                param_name, param_code, parent_id, orderby))
            data = {
                "status": 1,
                "message": u"新增参数配置成功",
                "data": []
            }
        except Exception as e:
            data = {
                "status": 0,
                "message": u"新增参数配置失败",
                "data": []
            }
    jsonstr = json.dumps(data)
    return HttpResponse(jsonstr, content_type="application/json")


# 功能：修改桥梁参数信息
# 输入参数：request
# 返回参数：response
@authjson
def QLXJ_QLCS_MODSAVE(request):
    param_name = request.POST['param_name']
    param_code = request.POST['param_code']
    xparam_id = request.POST['param_id']
    param_id = int(xparam_id)
    xorderby = request.POST['orderby']
    orderby = int(xorderby)
    try:
        row = MySQLDB.CommitMySql(
            "UPDATE t_sys_param set param_name = '%s',param_code = '%s',orderby = '%s' where param_id='%s' and status=1;" % (
            param_name, param_code, orderby, param_id))
        data = {
            "status": 1,
            "message": u"修改成功",
            "data": []
        }
    except Exception as e:
        data = {
            "status": 0,
            "message": u"修改失败",
            "data": []
        }
    jsonstr = json.dumps(data)
    return HttpResponse(jsonstr, content_type="application/json")


# 功能：删除桥梁参数信息
# 输入参数：request
# 返回参数：response
@authjson
def QLXJ_QLCS_DEL(request):
    xparam_id = request.GET['param_id']
    param_id = int(xparam_id)
    try:
        row1 = MySQLDB.SelectMySql("SELECT count(1) FROM t_sys_param where status=1 and parent_id='%s';" % (param_id))
        flag = row1[0][0]
        if flag == 0:
            try:
                row = MySQLDB.CommitMySql("UPDATE t_sys_param set status=0 where param_id='%s';" % (param_id))
                data = {
                    "status": 1,
                    "message": u"删除成功",
                    "data": []
                }
            except Exception as e:
                data = {
                    "status": 0,
                    "message": u"删除失败",
                    "data": []
                }
        else:
            data = {
                "status": 0,
                "message": u"请先删子节点",
                "data": []
            }
    except Exception as e:
        data = {
            "status": 0,
            "message": u"查询数据库失败",
            "data": []
        }
    jsonstr = json.dumps(data)
    return HttpResponse(jsonstr, content_type="application/json")


# 功能：新增属性参数信息
# 输入参数：request
# 返回参数：response
@authjson
def QLXJ_SXCS_ADD(request):
    name = request.POST['name']
    remark = request.POST['remark']
    xparam_type = request.POST['param_type']
    param_type = int(xparam_type)
    xorderby = request.POST['orderby']
    orderby = int(xorderby)
    try:
        row = MySQLDB.CommitMySql(
            "INSERT INTO t_sys_attribute (name,param_type,remark,status,orderby) VALUES ('%s',%s,'%s',1,%s);" % (
            name, param_type, remark, orderby))
        data = {
            "status": 1,
            "message": u"新增参数配置成功",
            "data": []
        }
    except Exception as e:
        data = {
            "status": 0,
            "message": u"新增参数配置失败",
            "data": []
        }
    jsonstr = json.dumps(data)
    return HttpResponse(jsonstr, content_type="application/json")


# 功能：修改属性参数信息
# 输入参数：request
# 返回参数：response
@authjson
def QLXJ_SXCS_MODSAVE(request):
    name = request.POST['name']
    remark = request.POST['remark']
    xparam_type = request.POST['param_type']
    param_type = int(xparam_type)
    xorderby = request.POST['orderby']
    orderby = int(xorderby)
    xid = request.POST['id']
    xid = int(xid)
    try:
        row = MySQLDB.CommitMySql(
            "UPDATE t_sys_attribute set name = '%s',remark = '%s',param_type = '%s',orderby = '%s' where id= %s and status=1;" % (
            name, remark, param_type, orderby, xid))
        data = {
            "status": 1,
            "message": u"修改成功",
            "data": []
        }
    except Exception as e:
        data = {
            "status": 0,
            "message": u"修改失败",
            "data": []
        }
    jsonstr = json.dumps(data)
    return HttpResponse(jsonstr, content_type="application/json")


# 功能：删除属性参数信息
# 输入参数：request
# 返回参数：response
@authjson
def QLXJ_SXCS_DEL(request):
    xid = request.GET['id']
    xid = int(xid)
    try:
        # 增加验证，当该属性被使用时，不得删除
        valid = MySQLDB.SelectMySql("select 1 from t_disease_attribute where attribute_id = %s union select 1 from t_parts_attribute where attribute_id = %s ;" % (xid, xid))
        if len(valid):
            raise Exception(u"属性被使用中,不能删除")
        row = MySQLDB.CommitMySql("UPDATE t_sys_attribute set status = 0 where id= %s ;" % (xid))
        data = {
            "status": 1,
            "message": u"删除成功",
            "data": []
        }
    except Exception as e:
        data = {
            "status": 0,
            "message": u"删除失败:"+e.message,
            "data": []
        }
    jsonstr = json.dumps(data)
    return HttpResponse(jsonstr, content_type="application/json")


# 功能：文件上传
# 输入参数：request
# 返回参数：response
@csrf_exempt
def uploadFile(request):
    MD5StrJS = '1.csv'
    myFile = request.FILES.getlist("myfile", None)
    if not myFile:
        data = {
            "status": 0,
            "message": u"no file",
            "data": []
        }
    try:
        for f in myFile:
            destination = open(os.path.join("/", MD5StrJS), 'wb+')
            for chunk in f.chunks():
                destination.write(chunk)
            destination.close()
        data = {
            "status": 1,
            "message": u"cg",
            "data": []
        }
    except Exception as e:
        data = {
            "status": 0,
            "message": str(e),
            "data": []
        }
    # 对1.csv文件进行读取，并导入数据库

    jsonstr = json.dumps(data)
    return HttpResponse(jsonstr, content_type="application/json")


# 功能：文件导出
# 输入参数：request
# 返回参数：response
def download(request):
    def file_iterator(file_name, chunk_size=512):
        with open(file_name) as f:
            while True:
                c = f.read(chunk_size)
                if c:
                    yield c
                else:
                    break

    row = MySQLDB.SelectMySql(
        "SELECT param_id,param_name,parent_id,param_code,orderby FROM t_sys_param WHERE status=1;")
    with open('/1.csv', 'wb') as csvfile:
        csvfile.write(codecs.BOM_UTF8)
        fieldnames = ['id', 'name', 'parentid', 'code', 'orderby']
        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
        writer.writeheader()
        for i in range(len(row)):
            writer.writerow({'id': row[i][0], 'name': unicode(row[i][1]).encode('utf8'), 'parentid': row[i][2],
                             'code': unicode(row[i][3]).encode('utf8'), 'orderby': row[i][4]})
    FDName = '/1.csv'
    StrName = '1.csv'
    response = StreamingHttpResponse(file_iterator(FDName))
    response['Content-Type'] = 'application/octet-stream'
    response['Content-Disposition'] = 'attachment;filename="{0}"'.format(StrName)
    return response


# 功能：查询属性参数列表
# 输入参数：request
# 返回参数：response
@authjson
def QLXJ_SXCS_DATA(request):
    # 属性参数列表
    try:
        row = MySQLDB.SelectMySql(
            "SELECT id,name,param_type,orderby,remark from t_sys_attribute where status=1 ORDER BY orderby;")
        gsalist = []
        if len(row) > 0:
            for i in range(len(row)):
                info = {}
                info["id"] = row[i][0]
                info["name"] = row[i][1]
                info["param_type"] = row[i][2]
                info["orderby"] = row[i][3]
                info["remark"] = row[i][4]
                gsalist.append(info)
        data = {
            "status": 1,
            "message": u"查询属性参数列表成功",
            "data": gsalist
        }
    except Exception as e:
        data = {
            "status": 0,
            "message": u"查询属性参数列表失败",
            "data": []
        }
    jsonstr = json.dumps(data)
    return HttpResponse(jsonstr, content_type="application/json")


# 功能：获取病害标度列表信息
# 输入参数：request
# 返回参数：response
@authjson
def QLXJ_JCSJ_BHBD_index(request):
    try:
        row = MySQLDB.SelectMySql(
            "SELECT `disease_id`,`disease_name`,`orderby` from t_sys_disease where status = 1 ORDER BY orderby;")
        alist = []
        if len(row) > 0:
            for i in range(len(row)):
                info = {}
                info["disease_id"] = row[i][0]
                info["disease_name"] = row[i][1]
                info["orderby"] = row[i][2]
                alist.append(info)
        data = {
            "status": 1,
            "message": u"查询属性参数列表成功",
            "data": alist
        }
    except Exception as e:
        data = {
            "status": 0,
            "message": u"查询属性参数列表失败",
            "data": []
        }
    jsonstr = json.dumps(data)
    return HttpResponse(jsonstr, content_type="application/json")


# 功能：获取某个病害详细信息
# 输入参数：request
# 返回参数：response
@authjson
def QLXJ_JCSJ_BHBD_diseasedetails(request):
    xdisease_id = request.GET['disease_id']
    disease_id = int(xdisease_id)
    try:
        row = MySQLDB.SelectMySql(
            "SELECT `disease_scale_id`,`level`,`qualitative`,`quantitative`,`sumup` from t_sys_disease_scale where disease_id = %s and status = 1;" % (
            disease_id))
        alist = []
        if len(row) > 0:
            key = ["disease_scale_id", "level", "qualitative", "quantitative", "sumup"]
            for i in range(len(row)):
                info = {}
                for j in range(len(key)):
                    info[key[j]] = row[i][j]
                alist.append(info)
        data = {
            "status": 1,
            "message": u"查询成功",
            "data": alist
        }
    except Exception as e:
        data = {
            "status": 0,
            "message": str(e),
            "data": []
        }
    jsonstr = json.dumps(data)
    return HttpResponse(jsonstr, content_type="application/json")


# 功能：新增病害
# 输入参数：request
# 返回参数：response
@authjson
def QLXJ_JCSJ_BHBD_adddisease(request):
    disease_name = request.POST['disease_name']
    xorderby = request.POST['orderby']
    orderby = int(xorderby)
    try:
        row = MySQLDB.CommitMySql("INSERT INTO `t_sys_disease`(`disease_name`,`status`,`orderby`) select '%s',1,%s;" % (
        disease_name, orderby))
        data = {
            "status": 1,
            "message": u"新增成功",
            "data": []
        }
    except Exception as e:
        data = {
            "status": 0,
            "message": u'新增失败',
            "data": []
        }
    jsonstr = json.dumps(data)
    return HttpResponse(jsonstr, content_type="application/json")


# 功能：新增标度
# 输入参数：request
# 返回参数：response
@authjson
def QLXJ_JCSJ_BHBD_addBDSave(request):
    disease_id = request.POST['disease_id']
    xlevel = request.POST['level']
    level = int(xlevel)
    qualitative = request.POST['qualitative']
    quantitative = request.POST['quantitative']
    sumup = request.POST['sumup']
    try:
        # 验证病害标度唯一
        valid = MySQLDB.SelectMySql("select 1 from t_sys_disease_scale where status = 1 and disease_id = %s and level = %s;"%(disease_id,level))
        if len(valid):
            data = {
            "status": 0,
            "message": u"病害标度不能重复,请重新选择病害标度",
            "data": []
            }
        else:
            row = MySQLDB.CommitMySql(
            "INSERT INTO `t_sys_disease_scale`(`disease_id`,`level`,`qualitative`,`quantitative`,`sumup`,`status`)select '%s','%s','%s','%s','%s',1;" % (
            disease_id, level, qualitative, quantitative, sumup))
            data = {
                "status": 1,
                "message": u"新增成功",
                "data": []
            }
    except Exception as e:
        data = {
            "status": 0,
            "message": u'新增失败',
            "data": []
        }
    jsonstr = json.dumps(data)
    return HttpResponse(jsonstr, content_type="application/json")


# 功能：修改病害保存json
# 输入参数：request
# 返回参数：response
@authjson
def QLXJ_JCSJ_BHBD_MODBHSave(request):
    xdisease_id = request.POST['disease_id']
    disease_id = int(xdisease_id)
    xorderby = request.POST['orderby']
    orderby = int(xorderby)
    disease_name = request.POST['disease_name']
    try:
        row = MySQLDB.CommitMySql(
            "UPDATE t_sys_disease set `disease_name`='%s',`orderby`='%s' where disease_id = %s and status = 1;" % (
            disease_name, orderby, disease_id))
        data = {
            "status": 1,
            "message": u"修改病害成功",
            "data": []
        }
    except Exception as e:
        data = {
            "status": 0,
            "message": u'修改病害失败',
            "data": []
        }
    jsonstr = json.dumps(data)
    return HttpResponse(jsonstr, content_type="application/json")


# 功能：删除病害json
# 输入参数：request
# 返回参数：response
@authjson
def QLXJ_JCSJ_BHBD_DELBH(request):
    xdisease_id = request.GET['disease_id']
    disease_id = int(xdisease_id)
    try:
        row = MySQLDB.CommitMySql("UPDATE t_sys_disease set `status`=0 where disease_id = %s;" % (disease_id))
        data = {
            "status": 1,
            "message": u"删除病害成功",
            "data": []
        }
    except Exception as e:
        data = {
            "status": 0,
            "message": u'删除病害失败',
            "data": []
        }
    jsonstr = json.dumps(data)
    return HttpResponse(jsonstr, content_type="application/json")


# 功能：修改标度
# 输入参数：request
# 返回参数：response
@authjson
def QLXJ_JCSJ_BHBD_MODBDSAVE(request):
    disease_scale_id = request.POST['disease_scale_id']
    disease_id = request.POST['disease_id']
    xlevel = request.POST['level']
    level = int(xlevel)
    qualitative = request.POST['qualitative']
    quantitative = request.POST['quantitative']
    sumup = request.POST['sumup']
    try:
        # 验证病害标度唯一
        valid = MySQLDB.SelectMySql("select 1 from t_sys_disease_scale where status = 1 and disease_id = %s and level = %s and disease_scale_id != %s;"%(disease_id,level,disease_scale_id))
        if len(valid):
            data = {
            "status": 0,
            "message": u"病害标度不能重复,请重新选择病害标度",
            "data": []
            }
        else:
            row = MySQLDB.CommitMySql(
            "UPDATE t_sys_disease_scale set `disease_id`='%s',`level`='%s',`qualitative`='%s',`quantitative`='%s',`sumup`='%s' where disease_scale_id = %s;" % (
            disease_id, level, qualitative, quantitative, sumup, disease_scale_id))
            data = {
                "status": 1,
                "message": u"修改标度成功",
                "data": []
            }
    except Exception as e:
        data = {
            "status": 0,
            "message": u'修改标度失败',
            "data": []
        }
    jsonstr = json.dumps(data)
    return HttpResponse(jsonstr, content_type="application/json")


# 功能：修改标度
# 输入参数：request
# 返回参数：response
@authjson
def QLXJ_JCSJ_BHBD_DELBD(request):
    disease_scale_id = request.GET['disease_scale_id']
    try:
        row = MySQLDB.CommitMySql(
            "UPDATE t_sys_disease_scale set `status`=0 where disease_scale_id = %s;" % (disease_scale_id))
        data = {
            "status": 1,
            "message": u"删除标度成功",
            "data": []
        }
    except Exception as e:
        data = {
            "status": 0,
            "message": u'删除标度失败',
            "data": []
        }
    jsonstr = json.dumps(data)
    return HttpResponse(jsonstr, content_type="application/json")


# 功能：部件权重数据列表
# 输入参数：request
# 返回参数：response
@authjson
def QLXJ_BJQZ_SJLB(request):
    try:
        row = MySQLDB.SelectMySql(
            "SELECT param_id,param_name,param_code from t_sys_param a where exists(select 1 from t_sys_param b where a.parent_id = b.param_id and b.param_code = 'bridgeType') and status=1 order by orderby;")
        gsalist = []
        if len(row) > 0:
            for i in range(len(row)):
                info = {}
                info["param_id"] = row[i][0]
                info["param_name"] = row[i][1]
                info["param_code"] = row[i][2]
                gsalist.append(info)
        data = {
            "status": 1,
            "message": u"查询部件权重数据列表成功",
            "data": gsalist
        }
    except Exception as e:
        data = {
            "status": 0,
            "message": u"查询部件权重数据列表失败",
            "data": []
        }
    jsonstr = json.dumps(data)
    return HttpResponse(jsonstr, content_type="application/json")


# 功能：部件权重数据列表详细信息
# 输入参数：request
# 返回参数：response
@authjson
def QLXJ_BJQZ_SJLB_detail(request):
    param_bridge_type_code = request.GET['param_bridge_type_code']

    try:
        row = MySQLDB.SelectMySql(
            "SELECT parts_no,bridge_parts_name,weight,position_code,parts_code from t_sys_bridge_parts where param_bridge_type_code = '%s';" % (
            param_bridge_type_code))
        gsalist = []
        if len(row) > 0:
            for i in range(len(row)):
                info = {}
                info["parts_no"] = row[i][0]
                info["bridge_parts_name"] = row[i][1]
                info["weight"] = str(row[i][2])
                info["position_code"] = row[i][3]
                info["parts_code"] = row[i][4]
                gsalist.append(info)
        data = {
            "status": 1,
            "message": u"查询部件权重数据列表详细信息成功",
            "data": gsalist
        }
    except Exception as e:
        data = {
            "status": 0,
            "message": u"查询部件权重数据列表详细信息失败",
            "data": []
        }
    jsonstr = json.dumps(data)
    return HttpResponse(jsonstr, content_type="application/json")


# 功能:部件列表信息
# 输入参数：request
# 返回参数：response
@authjson
def QLXJ_BJGL_LIST(request):
    data = {}
    try:
        row = MySQLDB.SelectMySql(
            "select parts_id,parts_code,parts_name,position_code from t_sys_parts where status = 1 order by parts_code;")
        list = []
        if len(row) > 0:
            for i in range(len(row)):
                info = {}
                info["parts_id"] = row[i][0]
                info["parts_code"] = row[i][1]
                info["parts_name"] = row[i][2]
                info["position_code"] = row[i][3]
                list.append(info)
        data = {
            "status": 1,
            "message": u"查询部件列表成功",
            "data": list
        }

    except Exception as e:
        data = {
            "status": 0,
            "message": u"查询部件权重数据列表详细信息失败",
            "data": []
        }
    return JsonResponse(data)


# 功能：新增部件页面跳转
# 传入参数：request
# 返回参数：responese
@auth
def QLXJ_BJGL_ADD(request):
    return render(request, "QLGL/ZDSJ/QLGL_ZDSJ_BJLB_ADD.html", {})


# 功能：新增部件保存
# 传入参数：request
# 返回参数：responese
@authjson
def QLXJ_BJGL_ADDSAVE(request):
    data = {}
    partsName = request.POST["parts_name"]
    partsCode = request.POST["parts_code"]
    positionCode = request.POST["position_code"]
    message = ""
    status = 0
    try:
        # 数据校验
        # name和no均不可重复
        valid = MySQLDB.SelectMySql(
            "select 1 from t_sys_parts where status = 1 and parts_name = '%s' ;" % (
            partsName))
        if len(valid) > 0:
            message = u"部件名重复,请重新输入"
        valid = MySQLDB.SelectMySql(
            "select 1 from t_sys_parts where status = 1 and parts_code = '%s';" % (
            partsCode))
        if len(valid) > 0:
            message = u"部件编码重复,请重新输入"
        if message == "":
            row = MySQLDB.CommitMySql(
                "INSERT INTO t_sys_parts (`parts_name`,`parts_code`,`status`,`position_code`) value ('%s','%s',%s,'%s');" % (
                partsName, partsCode, 1, positionCode))
            status = 1
            message = u"新增部件成功"
        data = {
            "status": status,
            "message": message,
            "data": []
        }

    except Exception as e:
        data = {
            "status": 0,
            "message": u"新增部件信息失败",
            "data": []
        }
    return JsonResponse(data)
    # return render(request, "test.html", {"resData":partsName})


# 功能：修改部件页面跳转
# 传入参数：request
# 返回参数：responese
@auth
def QLXJ_BJGL_MOD(request):
    partsId = request.GET["parts_id"]
    try:
        row = MySQLDB.SelectMySql(
            "select parts_id,parts_code,parts_name,position_code from t_sys_parts where status = 1 and parts_id =%s order by parts_code;" % partsId)
        list = []
        if len(row) > 0:
            for i in range(len(row)):
                info = {}
                info["parts_id"] = row[i][0]
                info["parts_code"] = row[i][1]
                info["parts_name"] = row[i][2]
                info["position_code"] = int(row[i][3])
                list.append(info)
    except:
        list = []
    return render(request, "QLGL/ZDSJ/QLGL_ZDSJ_BJLB_MODIFY.html", {"list": list})


# 功能：修改部件保存
# 传入参数：request
# 返回参数：responese
@authjson
def QLXJ_BJGL_MODSAVE(request):
    partsId = request.POST["parts_id"]
    partsName = request.POST["parts_name"]
    partsCode = request.POST["parts_code"]
    positionCode = request.POST["position_code"]
    message = ""
    status = 0
    try:
        # if 1==1:
        # 数据校验
        # name和no均不可重复
        valid = MySQLDB.SelectMySql(
            "select 1 from t_sys_parts where status = 1 and parts_name = '%s' and parts_id <> %s;" % (
            partsName, partsId))
        if len(valid) > 0:
            message = u"部件名重复,请重新输入"
        valid = MySQLDB.SelectMySql(
            "select 1 from t_sys_parts where status = 1 and parts_code = '%s' and parts_id <> %s;" % (
            partsCode, partsId))
        if len(valid) > 0:
            message = u"部件编码重复,请重新输入"
        if message == "":
            MySQLDB.CommitMySql(
                "UPDATE t_sys_parts set parts_name='%s',parts_code='%s',position_code='%s',update_date=now() WHERE parts_id = %s;" % (
                partsName, partsCode, positionCode, partsId))
            status = 1
            message = u"修改部件数据成功"
        data = {
            "status": status,
            "message": message,
            "data": []
        }

    except Exception as e:
        data = {
            "status": 0,
            "message": u"修改部件数据失败",
            "data": []
        }
    return JsonResponse(data)


# 功能：删除部件
# 传入参数：request
# 返回参数：responese
@authjson
def QLXJ_BJGL_DEL(request):
    partsId = request.GET["parts_id"]
    try:
        row = MySQLDB.CommitMySql("UPDATE t_sys_parts set status=0,update_date=now() WHERE parts_id = %s;" % (partsId))
        data = {
            "status": 1,
            "message": u"部件删除成功",
            "data": []
        }

    except Exception as e:
        data = {
            "status": 0,
            "message": u"部件删除失败",
            "data": []
        }
    return JsonResponse(data)


# 功能：病害映射页面
# 传入参数：request
# 返回参数：responese
@auth
def QLXJ_YSGL_BHYS_INDEX(request):
    return render(request, "QLGL/ZDSJ/QLGL_ZDSJ_BHYS.html", {})


# 功能：病害部件页面
# 传入参数：request
# 返回参数：responese
@auth
def QLXJ_YSGL_BHBJ(request):
    list = []
    diseaseId = request.GET["disease_id"]
    try:
        row = MySQLDB.SelectMySql("select parts_code,parts_name from t_sys_parts where status = 1;")
        if len(row) > 0:
            for i in range(len(row)):
                info = {}
                info["parts_code"] = row[i][0]
                info["parts_name"] = row[i][1]
                list.append(info)
    except Exception as e:
        pass
    return render(request, "QLGL/ZDSJ/QLGL_ZDSJ_BHYS_BJ.html", {"list": list, "diseaseId": diseaseId})


# 功能：获取部件列表
# 传入参数：request
# 返回参数：responese
@auth
def QLXJ_YSGL_BHBJ_BJLIST(request):
    data = {}
    try:
        row = MySQLDB.SelectMySql("select parts_code,parts_name from t_sys_parts where status = 1;")
        list = []
        if len(row) > 0:
            for i in range(len(row)):
                info = {}
                info["parts_code"] = row[i][0]
                info["parts_name"] = row[i][1]
                list.append(info)
        data = {
            "status": 1,
            "message": u"获取部件列表成功",
            "data": list
        }

    except Exception as e:
        data = {
            "status": 0,
            "message": u"获取部件列表失败",
            "data": []
        }
    return JsonResponse(data)


# 功能：获取病害已对应部件列表
# 传入参数：request
# 返回参数：responese
@authjson
def QLXJ_YSGL_BHBJ_BJSEL(request):
    data = {}
    diseaseId = request.GET["disease_id"]
    try:
        row = MySQLDB.SelectMySql("select parts_code from t_disease_parts where disease_id = %s;" % diseaseId)
        list = []
        if len(row) > 0:
            for i in range(len(row)):
                info = {}
                info["parts_code"] = row[i][0]
                list.append(info)
        data = {
            "status": 1,
            "message": u"获取病害已对应部件列表成功",
            "data": list
        }

    except Exception as e:
        data = {
            "status": 0,
            "message": u"获取病害已对应部件列表失败",
            "data": []
        }
    return JsonResponse(data)


# 功能：保存病害-部件对应
# 传入参数：request
# 返回参数：responese
@authjson
def QLXJ_YSGL_BHBJ_SAVE(request):
    data = {}
    diseaseId = request.GET["disease_id"]
    partsCodes = request.GET["parts_codes"]
    try:
        MySQLDB.CommitMySql("DELETE FROM t_disease_parts where disease_id = %s;" % diseaseId)
        if len(partsCodes) > 0:
            updataSql = ""
            temp = partsCodes.split('|')
            for t in temp:
                if len(t) > 0:
                    MySQLDB.CommitMySql(
                        "insert into t_disease_parts (disease_id,parts_code) values (%s,%s);" % (diseaseId, t))
        data = {
            "status": 1,
            "message": u"保存数据成功",
            "data": []
        }

    except Exception as e:
        data = {
            "status": 0,
            "message": u"保存数据失败",
            "data": []
        }
    return JsonResponse(data)


# 功能：打开病害-材料对应页面
# 传入参数：request
# 返回参数：responese
@auth
def QLXJ_YSGL_BHCL(request):
    diseaseId = request.GET["disease_id"]
    deckPavement = []
    material = []
    upperMaterial = []
    towerBasisMaterial = []  # 桥塔基础材料
    towerMaterial = []  # 桥塔材料
    try:
        # row = MySQLDB.SelectMySql(
        #     "select param_id,param_name from t_sys_param a where exists(select 1 from t_sys_param b where a.parent_id = b.param_id and b.param_code in ('upperMaterial')) order by parent_id,orderby;")
        # if len(row) > 0:
        #     for i in range(len(row)):
        #         info = {}
        #         info["param_id"] = row[i][0]
        #         info["param_name"] = row[i][1]
        #         deckPavement.append(info)
        #
        # row1 = MySQLDB.SelectMySql(
        #     "select param_id,param_name from t_sys_param a where exists(select 1 from t_sys_param b where a.parent_id = b.param_id and b.param_code in ('material')) order by parent_id,orderby;")
        # if len(row) > 0:
        #     for i in range(len(row1)):
        #         info = {}
        #         info["param_id"] = row1[i][0]
        #         info["param_name"] = row1[i][1]
        #         material.append(info)
        #
        # row2 = MySQLDB.SelectMySql(
        #     "select param_id,param_name from t_sys_param a where exists(select 1 from t_sys_param b where a.parent_id = b.param_id and b.param_code in ('deckPavement')) order by parent_id,orderby;")
        # if len(row) > 0:
        #     for i in range(len(row2)):
        #         info = {}
        #         info["param_id"] = row2[i][0]
        #         info["param_name"] = row2[i][1]
        #         upperMaterial.append(info)
        deckPavement = Common.GetParamInfoByCode("upperMaterial")
        material = Common.GetParamInfoByCode("material")
        upperMaterial = Common.GetParamInfoByCode("deckPavement")
        towerBasisMaterial = Common.GetParamInfoByCode("basisMaterial")  # 桥塔基础材料
        towerMaterial = Common.GetParamInfoByCode("towerMaterial")  # 桥塔材料
    except Exception as e:
        pass
    return render(request, "QLGL/ZDSJ/QLGL_ZDSJ_BHYS_CL.html",
                  {"deckPavement": deckPavement, "material": material, "upperMaterial": upperMaterial, "towerBasisMaterial": towerBasisMaterial, "towerMaterial": towerMaterial,
                   "diseaseId": diseaseId})


# 功能：获取材料清单
# 传入参数：request
# 返回参数：responese
@authjson
def QLXJ_YSGL_BHCL_CLLIST(request):
    data = {}
    try:
        row = MySQLDB.SelectMySql(
            "select param_id,param_name from t_sys_param a where exists(select 1 from t_sys_param b where a.parent_id = b.param_id and b.param_code in ('upperMaterial','material','deckPavement','basisMaterial','towerMaterial')) order by parent_id,orderby;")
        list = []
        if len(row) > 0:
            for i in range(len(row)):
                info = {}
                info["param_id"] = row[i][0]
                info["param_name"] = row[i][1]
                list.append(info)
        data = {
            "status": 1,
            "message": u"获取材料列表成功",
            "data": list
        }

    except Exception as e:
        data = {
            "status": 0,
            "message": u"获取材料列表失败",
            "data": []
        }
    return JsonResponse(data)


# 功能：获取已对应病害材料清单
# 传入参数：request
# 返回参数：responese
@authjson
def QLXJ_YSGL_BHCL_CLSEL(request):
    data = {}
    diseaseId = request.GET["disease_id"]
    try:
        row = MySQLDB.SelectMySql("select param_material_id from t_disease_material where disease_id=%s;" % diseaseId)
        list = []
        if len(row) > 0:
            for i in range(len(row)):
                info = {}
                info["param_id"] = row[i][0]
                list.append(info)
        data = {
            "status": 1,
            "message": u"获取病害材料列表成功",
            "data": list
        }

    except Exception as e:
        data = {
            "status": 0,
            "message": u"获取病害材料列表失败",
            "data": [diseaseId]
        }
    return JsonResponse(data)


# 功能：保存病害材料对应数据
# 传入参数：request
# 返回参数：responese
@authjson
def QLXJ_YSGL_BHCL_SAVE(request):
    data = {}
    diseaseId = request.GET["disease_id"]
    ids = request.GET["param_ids"]
    try:
        MySQLDB.CommitMySql("DELETE FROM t_disease_material where disease_id = %s;" % diseaseId)
        if len(ids) > 0:
            temp = ids.split('|')
            for t in temp:
                if len(t) > 0:
                    MySQLDB.CommitMySql(
                        "insert into t_disease_material (disease_id,param_material_id) values (%s,%s);" % (
                        diseaseId, t))
        data = {
            "status": 1,
            "message": u"保存数据成功",
            "data": []
        }

    except Exception as e:
        data = {
            "status": 0,
            "message": u"保存数据失败",
            "data": []
        }
    return JsonResponse(data)


# 功能：打开病害桥型对应页面
# 传入参数：request
# 返回参数：responese
@auth
def QLXJ_YSGL_BHQX(request):
    diseaseId = request.GET["disease_id"]
    list = []
    try:
        row = MySQLDB.SelectMySql(
            "select param_id,param_name from t_sys_param a where exists(select 1 from t_sys_param b where a.parent_id = b.param_id and b.param_code = 'bridgeType') and status=1 order by orderby;")
        if len(row) > 0:
            for i in range(len(row)):
                info = {}
                info["param_id"] = row[i][0]
                info["param_name"] = row[i][1]
                list.append(info)

    except Exception as e:
        pass
    return render(request, "QLGL/ZDSJ/QLGL_ZDSJ_BHYS_QX.html", {"list": list, "diseaseId": diseaseId})


# 功能：桥型列表
# 传入参数：request
# 返回参数：responese
@authjson
def QLXJ_YSGL_BHQX_QXLIST(request):
    data = {}
    try:
        row = MySQLDB.SelectMySql(
            "select param_id,param_name from t_sys_param a where exists(select 1 from t_sys_param b where a.parent_id = b.param_id and b.param_code = 'bridgeType') and status=1 order by orderby;")
        list = []
        if len(row) > 0:
            for i in range(len(row)):
                info = {}
                info["param_id"] = row[i][0]
                info["param_name"] = row[i][1]
                list.append(info)
        data = {
            "status": 1,
            "message": u"获取信息成功",
            "data": list
        }

    except Exception as e:
        data = {
            "status": 0,
            "message": u"获取信息失败",
            "data": []
        }
    return JsonResponse(data)


# 功能：已选择病害-桥型信息
# 传入参数：request
# 返回参数：responese
@authjson
def QLXJ_YSGL_BHQX_QXSEL(request):
    data = {}
    diseaseId = request.GET["disease_id"]
    try:
        row = MySQLDB.SelectMySql(
            "select param_bridge_type_id from t_disease_bridge_type where disease_id = %s;" % diseaseId)
        list = []
        if len(row) > 0:
            for i in range(len(row)):
                info = {}
                info["bridge_type_id"] = row[i][0]
                list.append(info)
        data = {
            "status": 1,
            "message": u"获取信息成功",
            "data": list
        }

    except Exception as e:
        data = {
            "status": 0,
            "message": u"获取信息失败",
            "data": []
        }
    return JsonResponse(data)


# 功能：保存病害桥型对应数据
# 传入参数：request
# 返回参数：responese
@authjson
def QLXJ_YSGL_BHQX_SAVE(request):
    data = {}
    diseaseId = request.GET["disease_id"]
    ids = request.GET["bridge_type_ids"]
    try:
        MySQLDB.CommitMySql("DELETE FROM t_disease_bridge_type where disease_id = %s;" % diseaseId)
        if len(ids) > 0:
            temp = ids.split('|')
            for t in temp:
                if len(t) > 0:
                    MySQLDB.CommitMySql(
                        "insert into t_disease_bridge_type (disease_id,param_bridge_type_id) values (%s,%s);" % (
                        diseaseId, t))
        data = {
            "status": 1,
            "message": u"保存数据成功",
            "data": []
        }

    except Exception as e:
        data = {
            "status": 0,
            "message": u"保存数据失败",
            "data": []
        }
    return JsonResponse(data)


# 功能：打开病害支座对应页面
# 传入参数：request
# 返回参数：responese
@auth
def QLXJ_YSGL_BHZZ(request):
    diseaseId = request.GET["disease_id"]
    list = []
    try:
        row = MySQLDB.SelectMySql(
            "select param_id,param_name from t_sys_param a where exists(select 1 from t_sys_param b where a.parent_id = b.param_id and b.param_code = 'supportType') and status=1 order by orderby;")
        if len(row) > 0:
            for i in range(len(row)):
                info = {}
                info["param_id"] = row[i][0]
                info["param_name"] = row[i][1]
                list.append(info)

    except Exception as e:
        pass
    return render(request, "QLGL/ZDSJ/QLGL_ZDSJ_BHYS_ZZ.html", {"list": list, "diseaseId": diseaseId})


# 功能：支座列表
# 传入参数：request
# 返回参数：responese
@authjson
def QLXJ_YSGL_BHZZ_ZZLIST(request):
    data = {}
    try:
        row = MySQLDB.SelectMySql(
            "select param_id,param_name from t_sys_param a where exists(select 1 from t_sys_param b where a.parent_id = b.param_id and b.param_code = 'supportType') and status=1 order by orderby;")
        list = []
        if len(row) > 0:
            for i in range(len(row)):
                info = {}
                info["param_id"] = row[i][0]
                info["param_name"] = row[i][1]
                list.append(info)
        data = {
            "status": 1,
            "message": u"获取信息成功",
            "data": list
        }

    except Exception as e:
        data = {
            "status": 0,
            "message": u"获取信息失败",
            "data": []
        }
    return JsonResponse(data)


# 功能：已选择病害-支座信息
# 传入参数：request
# 返回参数：responese
@authjson
def QLXJ_YSGL_BHZZ_ZZSEL(request):
    data = {}
    diseaseId = request.GET["disease_id"]
    try:
        row = MySQLDB.SelectMySql(
            "select param_support_type_id from t_disease_support_type where disease_id = %s;" % diseaseId)
        list = []
        if len(row) > 0:
            for i in range(len(row)):
                info = {}
                info["param_support_type_id"] = row[i][0]
                list.append(info)
        data = {
            "status": 1,
            "message": u"获取信息成功",
            "data": list
        }

    except Exception as e:
        data = {
            "status": 0,
            "message": u"获取信息失败",
            "data": []
        }
    return JsonResponse(data)


# 功能：保存病害支座对应数据
# 传入参数：request
# 返回参数：responese
@authjson
def QLXJ_YSGL_BHZZ_SAVE(request):
    data = {}
    diseaseId = request.GET["disease_id"]
    ids = request.GET["param_ids"]
    try:
        MySQLDB.CommitMySql("DELETE FROM t_disease_support_type where disease_id = %s;" % diseaseId)
        if len(ids) > 0:
            temp = ids.split('|')
            for t in temp:
                if len(t) > 0:
                    MySQLDB.CommitMySql(
                        "insert into t_disease_support_type (disease_id,param_support_type_id) values (%s,%s);" % (
                        diseaseId, t))
        data = {
            "status": 1,
            "message": u"保存数据成功",
            "data": []
        }

    except Exception as e:
        data = {
            "status": 0,
            "message": u"保存数据失败",
            "data": []
        }
    return JsonResponse(data)


# 功能：打开病害属性对应页面
# 传入参数：request
# 返回参数：responese
@auth
def QLXJ_YSGL_BHSS(request):
    diseaseId = request.GET["disease_id"]
    list = []
    try:
        row = MySQLDB.SelectMySql(
            "select id,CONCAT(name,'(',remark,')') from t_sys_attribute where param_type = 5 and status = 1 order by orderby;")
        if len(row) > 0:
            for i in range(len(row)):
                info = {}
                info["attr_id"] = row[i][0]
                info["attr_name"] = row[i][1]
                list.append(info)

    except Exception as e:
        pass
    return render(request, "QLGL/ZDSJ/QLGL_ZDSJ_BHYS_SX.html", {"list": list, "diseaseId": diseaseId})


# 功能：属性列表
# 传入参数：request
# 返回参数：responese
@authjson
def QLXJ_YSGL_BHSS_SSLIST(request):
    data = {}
    try:
        row = MySQLDB.SelectMySql(
            "select id,CONCAT(name,'(',remark,')') from t_sys_attribute where param_type = 5 and status = 1 order by orderby;")
        list = []
        if len(row) > 0:
            for i in range(len(row)):
                info = {}
                info["attr_id"] = row[i][0]
                info["attr_name"] = row[i][1]
                list.append(info)
        data = {
            "status": 1,
            "message": u"获取信息成功",
            "data": list
        }

    except Exception as e:
        data = {
            "status": 0,
            "message": u"获取信息失败",
            "data": []
        }
    return JsonResponse(data)


# 功能：已选择病害属性信息
# 传入参数：request
# 返回参数：responese
@authjson
def QLXJ_YSGL_BHSS_SSSEL(request):
    data = {}
    diseaseId = request.GET["disease_id"]
    try:
        row = MySQLDB.SelectMySql(
            "select attribute_id from t_disease_attribute where disease_id = %s and param_type = 5;" % diseaseId)
        list = []
        if len(row) > 0:
            for i in range(len(row)):
                info = {}
                info["attr_id"] = row[i][0]
                list.append(info)
        data = {
            "status": 1,
            "message": u"获取信息成功",
            "data": list
        }

    except Exception as e:
        data = {
            "status": 0,
            "message": u"获取信息失败",
            "data": []
        }
    return JsonResponse(data)


# 功能：保存病害属性对应数据
# 传入参数：request
# 返回参数：responese
@authjson
def QLXJ_YSGL_BHSS_SAVE(request):
    data = {}
    diseaseId = request.GET["disease_id"]
    ids = request.GET["attr_ids"]
    try:
        MySQLDB.CommitMySql("DELETE FROM t_disease_attribute where disease_id = %s AND param_type = 5;" % diseaseId)
        if len(ids) > 0:
            temp = ids.split('|')
            for t in temp:
                if len(t) > 0:
                    MySQLDB.CommitMySql(
                        "insert into t_disease_attribute (disease_id,attribute_id,param_type) select %s,id,param_type from t_sys_attribute where id = %s;" % (
                        diseaseId, t))
        data = {
            "status": 1,
            "message": u"保存数据成功",
            "data": []
        }

    except Exception as e:
        data = {
            "status": 0,
            "message": u"保存数据失败",
            "data": []
        }
    return JsonResponse(data)


# 功能：打开病害参数4对应页面
# 传入参数：request
# 返回参数：responese
@auth
def QLXJ_YSGL_BHCS4(request):
    list = []
    diseaseId = request.GET["disease_id"]
    try:
        row = MySQLDB.SelectMySql(
            "select id,name from t_sys_attribute where param_type = 4 and status = 1 order by orderby;")
        if len(row) > 0:
            for i in range(len(row)):
                info = {}
                info["attr_id"] = row[i][0]
                info["attr_name"] = row[i][1]
                list.append(info)

    except Exception as e:
        pass
    return render(request, "QLGL/ZDSJ/QLGL_ZDSJ_BHYS_CS.html", {"list": list, "diseaseId": diseaseId})


# 功能：参数列表
# 传入参数：request
# 返回参数：responese
@authjson
def QLXJ_YSGL_BHCS4_CSLIST(request):
    data = {}
    try:
        row = MySQLDB.SelectMySql(
            "select id,name from t_sys_attribute where param_type = 4 and status = 1 order by orderby;")
        list = []
        if len(row) > 0:
            for i in range(len(row)):
                info = {}
                info["attr_id"] = row[i][0]
                info["attr_name"] = row[i][1]
                list.append(info)
        data = {
            "status": 1,
            "message": u"获取信息成功",
            "data": list
        }

    except Exception as e:
        data = {
            "status": 0,
            "message": u"获取信息失败",
            "data": []
        }
    return JsonResponse(data)


# 功能：已选择病害-参数信息
# 传入参数：request
# 返回参数：responese
@authjson
def QLXJ_YSGL_BHCS4_CSSEL(request):
    data = {}
    diseaseId = request.GET["disease_id"]
    try:
        row = MySQLDB.SelectMySql(
            "select attribute_id from t_disease_attribute where disease_id = %s and param_type = 4;" % diseaseId)
        list = []
        if len(row) > 0:
            for i in range(len(row)):
                info = {}
                info["attr_id"] = row[i][0]
                list.append(info)
        data = {
            "status": 1,
            "message": u"获取信息成功",
            "data": list
        }

    except Exception as e:
        data = {
            "status": 0,
            "message": u"获取信息失败",
            "data": []
        }
    return JsonResponse(data)


# 功能：保存病害参数4对应数据
# 传入参数：request
# 返回参数：responese
@authjson
def QLXJ_YSGL_BHCS4_SAVE(request):
    data = {}
    diseaseId = request.GET["disease_id"]
    ids = request.GET["attr_ids"]
    try:
        MySQLDB.CommitMySql("DELETE FROM t_disease_attribute where disease_id = %s AND param_type = 4;" % diseaseId)
        if len(ids) > 0:
            temp = ids.split('|')
            for t in temp:
                if len(t) > 0:
                    MySQLDB.CommitMySql(
                        "insert into t_disease_attribute (disease_id,attribute_id,param_type) select %s,id,param_type from t_sys_attribute where id = %s;" % (
                        diseaseId, t))
        data = {
            "status": 1,
            "message": u"保存数据成功",
            "data": []
        }

    except Exception as e:
        data = {
            "status": 0,
            "message": u"保存数据失败",
            "data": []
        }
    return JsonResponse(data)


# 功能：位置映射页面
# 传入参数：request
# 返回参数：responese
@auth
def QLXJ_YSGL_WZYS(request):
    return render(request, "QLGL/ZDSJ/QLGL_ZDSJ_WZYS.html", {})


# 功能：位置映射-参数1页面
# 传入参数：request
# 返回参数：responese
@auth
def QLXJ_YSGL_WZYS_PARAM1(request):
    list = []
    partCode = request.GET["part_code"]
    try:
        row = MySQLDB.SelectMySql(
            "select id,name from t_sys_attribute where param_type=1 and status=1 order by orderby;")
        if len(row) > 0:
            for i in range(len(row)):
                info = {}
                info["attr_id"] = row[i][0]
                info["attr_name"] = row[i][1]
                list.append(info)

    except Exception as e:
        pass
    return render(request, "QLGL/ZDSJ/QLGL_ZDSJ_WZYS_CS1.html", {"list": list, "partCode": partCode})


# 功能：位置映射-参数2页面
# 传入参数：request
# 返回参数：responese
@auth
def QLXJ_YSGL_WZYS_PARAM2(request):
    list = []
    partCode = request.GET["part_code"]
    try:
        row = MySQLDB.SelectMySql(
            "select id,name from t_sys_attribute where param_type=2 and status=1 order by orderby;")
        if len(row) > 0:
            for i in range(len(row)):
                info = {}
                info["attr_id"] = row[i][0]
                info["attr_name"] = row[i][1]
                list.append(info)

    except Exception as e:
        pass
    return render(request, "QLGL/ZDSJ/QLGL_ZDSJ_WZYS_CS2.html", {"list": list, "partCode": partCode})


# 功能：位置映射-参数3页面
# 传入参数：request
# 返回参数：responese
@auth
def QLXJ_YSGL_WZYS_PARAM3(request):
    list = []
    partCode = request.GET["part_code"]
    try:
        row = MySQLDB.SelectMySql(
            "select id,name from t_sys_attribute where param_type=3 and status=1 order by orderby;")
        if len(row) > 0:
            for i in range(len(row)):
                info = {}
                info["attr_id"] = row[i][0]
                info["attr_name"] = row[i][1]
                list.append(info)

    except Exception as e:
        pass
    return render(request, "QLGL/ZDSJ/QLGL_ZDSJ_WZYS_CS3.html", {"list": list, "partCode": partCode})


# 功能：位置映射-已选择参数信息
# 传入参数：request
# 返回参数：responese
@authjson
def QLXJ_YSGL_WZYS_PARAMSEL(request):
    data = {}
    partCode = request.GET["part_code"]
    paramType = request.GET["param_type"]
    try:
        row = MySQLDB.SelectMySql(
            "select attribute_id from t_parts_attribute where param_type=%s and parts_code = %s;" % (
            paramType, partCode))
        list = []
        if len(row) > 0:
            for i in range(len(row)):
                info = {}
                info["attr_id"] = row[i][0]
                list.append(info)
        data = {
            "status": 1,
            "message": u"获取信息成功",
            "data": list
        }

    except Exception as e:
        data = {
            "status": 0,
            "message": u"获取信息失败",
            "data": []
        }
    return JsonResponse(data)


# 功能：保存位置映射-参数对应数据
# 传入参数：request
# 返回参数：responese
@authjson
def QLXJ_YSGL_WZYS_PARAM_SAVE(request):
    data = {}
    partCode = request.GET["part_code"]
    paramType = request.GET["param_type"]
    ids = request.GET["attr_ids"]
    try:
        MySQLDB.CommitMySql(
            "DELETE FROM t_parts_attribute where parts_code = %s and param_type=%s;" % (partCode, paramType))
        if len(ids) > 0:
            temp = ids.split('|')
            for t in temp:
                if len(t) > 0:
                    MySQLDB.CommitMySql(
                        "insert into t_parts_attribute (parts_code,param_type,attribute_id) values(%s,%s,%s)" % (
                        partCode, paramType, t))
        data = {
            "status": 1,
            "message": u"保存数据成功",
            "data": []
        }

    except Exception as e:
        data = {
            "status": 0,
            "message": u"保存数据失败",
            "data": []
        }
    return JsonResponse(data)


# 功能：路线管理首页
# 传入参数：request
# 返回参数：response
@auth
def QLXJ_ZDSJ_LXGL_index(request):
    return render(request, "QLGL/QLSJ/QLGL_QLSJ_LXLB.html", {})


# 功能：路线管理json
# 传入参数：request
# 返回参数：response
@authjson
def QLXJ_ZDSJ_LXGL(request):
    list = []
    try:
        row = MySQLDB.SelectMySql(
            "select line_id,line_name,line_no,b.param_name as line_type,(select count(1) from t_sys_section c where status = 1 and c.line_id=a.line_id ) from t_sys_line a inner join t_sys_param b on a.param_line_level = b.param_id where a.status = 1;")
        if len(row) > 0:
            for i in range(len(row)):
                info = {}
                info["line_id"] = row[i][0]
                info["line_name"] = row[i][1]
                info["line_no"] = row[i][2]
                info["line_type"] = row[i][3]
                info["line_status"] = row[i][4]
                list.append(info)
        data = {
            "status": 1,
            "message": u'查询线路管理信息成功',
            "data": list
        }
    except Exception as e:
        data = {
            "status": 0,
            "message": u'查询线路管理信息失败',
            "data": []
        }
    return JsonResponse(data)


# 功能：新增路线
# 传入参数：request
# 返回参数：response
@auth
def QLXJ_ZDSJ_LXGL_ADD(request):
    lineLevel = []
    try:
        lineLevel = Common.GetParamInfoByCode("lineLevel")
    except Exception as e:
        pass
    return render(request, "QLGL/QLSJ/QLGL_QLSJ_LXLB_ADD.html", {"lineLevel": lineLevel})


# 功能：新增路线数据保存
# 传入参数：request
# 返回参数：response
@authjson
def QLXJ_ZDSJ_LXGL_ADDSAVE(request):
    data = {}
    lineName = request.POST["line_name"]
    lineNo = request.POST["line_no"]
    lineType = request.POST["line_type"]
    status = 0
    message = ""
    try:
        # 数据校验
        # name和no均不可重复
        valid = MySQLDB.SelectMySql("select 1 from t_sys_line where status = 1 and line_name = '%s';" % lineName)
        if len(valid) > 0:
            message = u"线路名称重复,请重新输入"
        valid = MySQLDB.SelectMySql("select 1 from t_sys_line where status = 1 and line_no = '%s';" % lineNo)
        if len(valid) > 0:
            message = u"线路编号重复,请重新输入"
        if message == "":
            MySQLDB.CommitMySql(
                " insert into t_sys_line (line_name,line_no,param_line_level,status) values('%s','%s',%s,1)" % (
                lineName, lineNo, lineType))
            status = 1
            message = u"新增路线数据成功"
        data = {
            "status": status,
            "message": message,
            "data": []
        }

    except Exception as e:
        data = {
            "status": 0,
            "message": u"新增路线数据失败",
            "data": []
        }
    return JsonResponse(data)


# 功能：修改路线
# 传入参数：request
# 返回参数：response
@auth
def QLXJ_ZDSJ_LXGL_MOD(request):
    lineId = request.GET["line_id"]
    lineLevel = []
    try:
        lineLevel = Common.GetParamInfoByCode("lineLevel")
    except Exception as e:
        pass
    lineData = {}
    try:
        row = MySQLDB.SelectMySql(
            "select line_id,line_name,line_no,b.param_name as line_type from t_sys_line a inner join t_sys_param b on a.param_line_level = b.param_id where a.status = 1 and a.line_id = %s ;" % lineId)
        if len(row) > 0:
            for i in range(len(row)):
                lineData["line_id"] = row[i][0]
                lineData["line_name"] = row[i][1]
                lineData["line_no"] = row[i][2]
                lineData["line_type"] = row[i][3]

    except Exception as e:
        pass
    return render(request, "QLGL/QLSJ/QLGL_QLSJ_LXLB_MODIFY.html", {"lineData": lineData, "lineLevel": lineLevel})


# 功能：修改路线数据保存
# 传入参数：request
# 返回参数：response
@authjson
def QLXJ_ZDSJ_LXGL_MODSAVE(request):
    data = {}
    lineName = request.POST["line_name"]
    lineNo = request.POST["line_no"]
    lineType = request.POST["line_type"]
    lineId = request.POST["line_id"]
    status = 0
    message = ""
    try:
        # 数据校验
        # name和no均不可重复
        valid = MySQLDB.SelectMySql(
            "select 1 from t_sys_line where status = 1 and line_name = '%s' and line_id <> %s;" % (lineName, lineId))
        if len(valid) > 0:
            message = u"线路名称重复,请重新输入"
        valid = MySQLDB.SelectMySql(
            "select 1 from t_sys_line where status = 1 and line_no = '%s' and line_id <> %s;;" % (lineNo, lineId))
        if len(valid) > 0:
            message = u"线路编号重复,请重新输入"
        if message == "":
            MySQLDB.CommitMySql(
                "UPDATE t_sys_line set line_name='%s',line_no='%s',param_line_level='%s',update_date=now() where line_id = %s" % (
                lineName, lineNo, lineType, lineId))
            MySQLDB.CommitMySql(
                "UPDATE t_sys_section set line_name='%s',update_date=now() where line_id = %s" % (
                    lineName, lineId))
            MySQLDB.CommitMySql(
                "UPDATE t_bridge_base set line_name='%s',update_date=now() where line_id = %s" % (
                    lineName, lineId))
            status = 1
            message = u"修改路线数据成功"
        data = {
            "status": status,
            "message": message,
            "data": []
        }

    except Exception as e:
        data = {
            "status": 0,
            "message": u"保存数据失败",
            "data": []
        }
    return JsonResponse(data)


# 功能：删除路线数据
# 传入参数：request
# 返回参数：response
@authjson
def QLXJ_ZDSJ_LXGL_DEL(request):
    data = {}
    lineId = request.GET["line_id"]
    try:
        MySQLDB.CommitMySql("UPDATE t_sys_line set status=0,update_date=now() where line_id = %s" % (lineId))
        data = {
            "status": 1,
            "message": u"删除路线数据成功",
            "data": []
        }

    except Exception as e:
        data = {
            "status": 0,
            "message": u"删除路线数据失败",
            "data": []
        }
    return JsonResponse(data)


# 功能：路段管理首页
# 传入参数：request
# 返回参数：response
@auth
def QLXJ_ZDSJ_LDGL_index(request):
    return render(request, "QLGL/QLSJ/QLGL_QLSJ_LDLB.html", {})


# 功能：路段管理
# 传入参数：request
# 返回参数：response
@authjson
def QLXJ_ZDSJ_LDGL(request):
    list = []
    try:
        row = MySQLDB.SelectMySql(
            "select section_id,section_name,line_name,section_no,(select count(1) from t_bridge_base b where b.status=1 and b.section_id=a.section_id)  from t_sys_section a where a.status = 1;")
        if len(row) > 0:
            for i in range(len(row)):
                info = {}
                info["section_id"] = row[i][0]
                info["section_name"] = row[i][1]
                info["line_name"] = row[i][2]
                info["section_no"] = row[i][3]
                info["section_status"] = row[i][4]
                list.append(info)

        data = {
            "status": 1,
            "message": u'查询路段管理信息成功',
            "data": list
        }
    except Exception as e:
        data = {
            "status": 0,
            "message": u'查询路段管理信息失败',
            "data": []
        }
    return JsonResponse(data)


# 功能：新增路段
# 传入参数：request
# 返回参数：response
@auth
def QLXJ_ZDSJ_LDGL_ADD(request):
    list = []
    try:
        # row = MySQLDB.SelectMySql("select line_id,line_name from t_sys_line where status = 1;")
        # if len(row)>0:
        #     for i in range(len(row)):
        #         info = {}
        #         info["line_id"] = row[i][0]
        #         info["line_name"] = row[i][1]
        #         list.append(info)
        list = Common.GetLineInfoList()
    except Exception as e:
        pass
    return render(request, "QLGL/QLSJ/QLGL_QLSJ_LDLB_ADD.html", {"list": list})


# 功能：新增路段数据保存
# 传入参数：request
# 返回参数：response
@authjson
def QLXJ_ZDSJ_LDGL_ADDSAVE(request):
    data = {}
    sectionName = request.POST["section_name"]
    sectionNo = request.POST["section_no"]
    lineId = request.POST["line_id"]
    status = 0
    message = ""
    try:
        # 数据校验
        # name不可重复
        valid = MySQLDB.SelectMySql("select 1 from t_sys_line where status = 1 and line_id = %s;" % lineId)
        if len(valid) == 0:
            message = u"路线信息异常,请重新选择线路"
        valid = MySQLDB.SelectMySql(
            "select 1 from t_sys_section where status = 1 and section_name = '%s';" % sectionName)
        if len(valid) > 0:
            message = u"路段名称重复,请重新输入"
        if message == "":
            MySQLDB.CommitMySql(
                "INSERT INTO `t_sys_section`(`line_id`,`line_name`,`section_name`,`section_no`,`status`) select line_id,line_name,'%s','%s',1 from t_sys_line where status = 1 and line_id = %s;" % (
                sectionName, sectionNo, lineId))
            status = 1
            message = u"新增路线数据成功"
        data = {
            "status": status,
            "message": message,
            "data": []
        }
    except Exception as e:
        data = {
            "status": 0,
            "message": u"新增路线数据失败",
            "data": []
        }
    return JsonResponse(data)


# 功能：桥梁数据首页
# 传入参数：request
# 返回参数：response
@auth
def QLGL_QLSJ_index(request):
    return render(request, "QLGL/QLSJ/QLGL_QLSJ_index.html", {})


# 功能：修改路段
# 传入参数：request
# 返回参数：response
@auth
def QLXJ_ZDSJ_LDGL_MOD(request):
    sectionId = request.GET["section_id"]
    list = []
    sectionData = {}
    try:
        row = MySQLDB.SelectMySql(
            "select section_id,line_id,section_name,section_no from t_sys_section where status = 1 and section_id=%s;" % sectionId)
        if len(row) > 0:
            for i in range(len(row)):
                sectionData["section_id"] = row[i][0]
                sectionData["line_id"] = row[i][1]
                sectionData["section_name"] = row[i][2]
                sectionData["section_no"] = row[i][3]

        row1 = MySQLDB.SelectMySql("select line_id,line_name from t_sys_line where status = 1;")
        if len(row1) > 0:
            for i in range(len(row1)):
                info = {}
                info["line_id"] = row1[i][0]
                info["line_name"] = row1[i][1]
                list.append(info)

    except Exception as e:
        pass
    return render(request, "QLGL/QLSJ/QLGL_QLSJ_LDLB_MODIFY.html", {"list": list, "sectionData": sectionData})


# 功能：修改路段数据保存
# 传入参数：request
# 返回参数：response
@authjson
def QLXJ_ZDSJ_LDGL_MODSAVE(request):
    data = {}
    sectionId = request.POST["section_id"]
    sectionName = request.POST["section_name"]
    sectionNo = request.POST["section_no"]
    lineId = request.POST["line_id"]
    status = 0
    message = ""
    try:
        # 数据校验
        # name不可重复
        valid = MySQLDB.SelectMySql("select 1 from t_sys_line where status = 1 and line_id = %s;" % lineId)
        if len(valid) == 0:
            message = u"路线信息异常,请重新选择线路"
        valid = MySQLDB.SelectMySql(
            "select 1 from t_sys_section where status = 1 and section_name = '%s' and section_id <> %s;" % (
            sectionName, sectionId))
        if len(valid) > 0:
            message = u"路段名称重复,请重新输入"
        if message == "":
            MySQLDB.CommitMySql(
                "update t_sys_section set section_name = '%s',section_no = '%s',line_id = %s,line_name = (select line_name from t_sys_line where line_id = %s),update_date=now() where section_id = %s" % (
                sectionName, sectionNo, lineId, lineId, sectionId))
            MySQLDB.CommitMySql(
                "update t_bridge_base set section_name = '%s',update_date=now() where section_id = %s" % (
                    sectionName,sectionId))
            status = 1
            message = u"修改路段数据成功"
        data = {
            "status": status,
            "message": message,
            "data": []
        }

    except Exception as e:
        data = {
            "status": 0,
            "message": u"修改数据失败",
            "data": []
        }
    return JsonResponse(data)


# 功能：删除路线数据
# 传入参数：request
# 返回参数：response
@authjson
def QLXJ_ZDSJ_LDGL_DEL(request):
    data = {}
    sectionId = request.GET["section_id"]
    try:
        MySQLDB.CommitMySql("UPDATE t_sys_section set update_date=now() , status=0 where section_id = %s" % (sectionId))
        data = {
            "status": 1,
            "message": u"删除路段数据成功",
            "data": []
        }

    except Exception as e:
        data = {
            "status": 0,
            "message": u"删除路段数据失败",
            "data": []
        }
    return JsonResponse(data)


# 功能：桥梁列表
# 传入参数：request
# 返回参数：response
@auth
def QLXJ_QLLB(request):
    return render(request, "QLGL/QLSJ/QLGL_QLSJ_QLLB.html", {})


# 功能：桥梁一览
# 传入参数：request
# 返回参数：response
@authjson
def QLXJ_QLLB_QLLIST(reqeust):
    data = {
        "status": 0,
        "message": "查询失败",
        "data": []
    }
    list = []
    try:
        # 获取桥梁信息
        row1 = MySQLDB.SelectMySql(
            "SELECT bridge_id,bridge_code,bridge_name,b.param_name,c.line_no,a.line_name,section_id,section_name,(select count(1)  from t_bridge_upperpart where bridge_id = a.bridge_id ) from t_bridge_base a inner join t_sys_param b on a.param_bridge_category = b.param_id  inner join t_sys_line c on a.line_id = c.line_id where a.status =1;")
        if len(row1) > 0:
            for i in range(len(row1)):
                # row = MySQLDB.SelectMySql("select 1  from t_bridge_upperpart where bridge_id = %s and status=1;" % row1[i][0])
                info = {}
                info["bridge_id"] = row1[i][0]
                info["bridge_code"] = row1[i][1]
                info["bridge_name"] = row1[i][2]
                info["param_name"] = row1[i][3]
                info["line_id"] = row1[i][4]
                info["line_name"] = row1[i][5]
                info["section_id"] = row1[i][6]
                info["section_name"] = row1[i][7]
                info["uperpart"] = row1[i][8]
                list.append(info)
        data = {
            "status": 1,
            "message": "查询成功",
            "data": list
        }
    except Exception as e:
        pass
    return JsonResponse(data)


# 功能：新增桥梁
# 传入参数：request
# 返回参数：response
@auth
def QLXJ_QLLB_ADD(request):
    line = []  # 路线
    section = []  # 路段
    lineLevel = []  # 路线等级
    funcType = []  # 功能类型
    designLoad = []  # 设计载荷
    approachAlignment = [{"name": u"直线","id":"1"}, {"name": u"曲线","id":"2"}]  # 引道线形
    supportType = []  # 支座类型
    pierStyle = []  # 桥墩形式
    material = []  # 桥墩、桥台材料
    form = []  # 桥墩、桥台、桥塔基础形式
    abutmentStyle = []  # 桥台形式
    deckPavement = []  # 桥面铺装
    expJoint = []  # 伸缩缝类型
    facilities = []  # 左\右侧防护设施
    bridgetype = []  # 桥型
    deckboardposition = []  # 桥面板位
    upperForceForm = []  # 上部结构受力形式
    upperStructureForm = []  # 上部结构形式

    towerBasisMaterial = []  # 桥塔基础材料
    towerStructureForm = []  # 桥塔结构形式
    towerMaterial = []  # 桥塔材料
    try:
        line = Common.GetLineInfoList()
        section = Common.GetSectionInfoList()
        lineLevel = Common.GetParamInfoByCode("lineLevel")  # 路线等级
        funcType = Common.GetParamInfoByCode("funcType")  # 功能类型
        designLoad = Common.GetParamInfoByCode("designLoad")  # 设计载荷
        supportType = Common.GetParamInfoByCode("supportType")  # 支座类型:
        pierStyle = Common.GetParamInfoByCode("pierStyle")  # 桥墩形式
        material = Common.GetParamInfoByCode("material")  # 桥墩、桥台材料
        form = Common.GetParamInfoByCode("form")  # 桥墩、桥台、桥塔基础形式
        abutmentStyle = Common.GetParamInfoByCode("style")  # 桥台形式
        deckPavement = Common.GetParamInfoByCode("deckPavement")  # 桥面铺装
        expJoint = Common.GetParamInfoByCode("expJoint")  # 伸缩缝类型
        facilities = Common.GetParamInfoByCode("facilities")  # 左\右侧防护设施
        bridgecategory = Common.GetParamInfoByCode("bridgeCategory")  # 桥梁类型
        bridgetype = Common.GetParamInfoByCode("bridgetype")  # 桥型
        deckboardposition = Common.GetParamInfoByCode("deckboardposition")  # 桥面板位
        upperForceForm = Common.GetParamInfoByCode("upperForceForm")  # 上部结构受力形式
        upperStructureForm = Common.GetParamInfoByCode("upperStructureForm")  # 上部结构形式

        towerBasisMaterial = Common.GetParamInfoByCode("basisMaterial")  # 桥塔基础材料
        towerStructureForm = Common.GetParamInfoByCode("towerStructureForm")  # 桥塔结构形式
        towerMaterial = Common.GetParamInfoByCode("towerMaterial")  # 桥塔材料
    except Exception as e:
        pass
    return render(request, "QLGL/QLSJ/QLGL_QLSJ_QLLB_ADD.html",
                  {"line": line, "section": section, "lineLevel": lineLevel, "funcType": funcType,
                   "designLoad": designLoad, "approachAlignment": approachAlignment, "supportType": supportType,
                   "pierStyle": pierStyle, "material": material, "form": form, "abutmentStyle": abutmentStyle,
                   "deckPavement": deckPavement, "expJoint": expJoint, "facilities": facilities,
                   "bridgecategory": bridgecategory, "bridgetype": bridgetype, "deckboardposition": deckboardposition,
                   "upperForceForm": upperForceForm, "upperStructureForm": upperStructureForm, "towerBasisMaterial": towerBasisMaterial, "towerStructureForm": towerStructureForm, "towerMaterial": towerMaterial})


# 功能：新增桥梁保存
# 传入参数：request
# 返回参数：response
@authjson
def QLXJ_QLLB_ADDSAVE(request):
    bridge_code = request.POST.get('bridge_code', '')
    bridge_name = request.POST.get('bridge_name', '')
    line_id = request.POST.get('line_id', '')
    if line_id.strip() == "":
        line_id = 0
    line_name = request.POST.get('line_name', '')
    section_id = request.POST.get('section_id', '')
    if section_id.strip() == "":
        section_id = 0
    section_name = request.POST.get('section_name', '')
    pile_no = request.POST.get('pile_no', '')
    maintenance_depart = request.POST.get('maintenance_depart', '')
    param_deck_pavement = request.POST.get('param_deck_pavement', '')
    if param_deck_pavement.strip() == "":
        param_deck_pavement = 0
    length = request.POST.get('length', 0)
    under_height = request.POST.get('under_height', 0)
    angle = request.POST.get('angle', 0)
    param_left_facilities = request.POST.get('param_left_facilities', '')
    if param_left_facilities.strip() == "":
        param_left_facilities = 0
    param_right_facilities = request.POST.get('param_right_facilities', '')
    if param_right_facilities.strip() == "":
        param_right_facilities = 0
    span_total = request.POST.get('span_total', '')
    if span_total.strip() == "":
        span_total = 0
    # under_pier_number = request.POST.get('under_pier_number', '')
    # under_abutment_number = request.POST.get('under_abutment_number', '')
    param_pier_material = request.POST.get('param_pier_material', '')
    if param_pier_material.strip() == "":
        param_pier_material = 0
    param_pier_style = request.POST.get('param_pier_style', '')
    if param_pier_style.strip() == "":
        param_pier_style = 0
    param_pier_base_form = request.POST.get('param_pier_base_form', '')
    if param_pier_base_form.strip() == "":
        param_pier_base_form = 0
    param_abutment_material = request.POST.get('param_abutment_material', '')
    if param_abutment_material.strip() == "":
        param_abutment_material = 0
    param_abutment_style = request.POST.get('param_abutment_style', '')
    if param_abutment_style.strip() == "":
        param_abutment_style = 0
    param_abutment_base_form = request.POST.get('param_abutment_base_form', '')
    if param_abutment_base_form.strip() == "":
        param_abutment_base_form = 0
    param_expansion_type = request.POST.get('param_expansion_type', '')
    if param_expansion_type.strip() == "":
        param_expansion_type = 0
    expansion_position = request.POST.get('expansion_position', '')
    param_support_type = request.POST.get('param_support_type', '')
    remark = request.POST.get('remark', '')
    # status = 1
    build_date = request.POST.get('build_date', '')
    param_line_level = request.POST.get('param_line_level', '')
    if param_line_level.strip() == "":
        param_line_level = 0
    # deck_width = request.POST.get('deck_width', 0)
    left_facilities_width = request.POST.get('left_facilities_width', 0)
    right_facilities_width = request.POST.get('right_facilities_width', 0)
    central_isolation_belt = request.POST.get('central_isolation_belt', 0)
    driveway_width = request.POST.get('driveway_width', '0')
    approach_width = request.POST.get('approach_width', '0')
    approach_road_width = request.POST.get('approach_road_width', '0')
    approach_linear = request.POST.get('approach_linear', '')
    under_cross_road = request.POST.get('under_cross_road', '')
    under_cross_road_no = request.POST.get('under_cross_road_no', '')
    # cross_situation_explain = request.POST.get('cross_situation_explain', '')
    param_func_type = request.POST.get('param_func_type', '')
    if param_func_type.strip() == "":
        param_func_type = 0
    param_design_load = request.POST.get('param_design_load', '')
    slope_angle = request.POST.get('slope_angle', '')
    build_depart = request.POST.get('build_depart', '')
    design_depart = request.POST.get('design_depart', '')
    construction_depart = request.POST.get('construction_depart', '')
    longitude = request.POST.get('longitude', '')
    latitude = request.POST.get('latitude', '')
    run_load = request.POST.get('run_load', '')
    bridge_total_width = request.POST.get('bridge_total_width', '')
    bridgecategory = request.POST.get('bridgecategory', '')
    status = 0
    message = ""

    # 2017-8-25 修改，增加参数
    bridge_longitudinal_slope = request.POST.get('bridge_longitudinal_slope', '')
    bridge_cross_slope = request.POST.get('bridge_cross_slope', '')
    deck_elevation = request.POST.get('deck_elevation', '')
    param_tower_structure_form = request.POST.get('param_tower_structure_form', 0)
    param_tower_material = request.POST.get('param_tower_material', 0)
    bridge_clear_height = request.POST.get('bridge_clear_height', '')
    earthquake_pac = request.POST.get('earthquake_pac', '')
    abutment_slope_protection = request.POST.get('abutment_slope_protection', '')
    pier_body = request.POST.get('pier_body', '')
    modulation_structure = request.POST.get('modulation_structure', '')
    water_level = request.POST.get('water_level', '')
    design_water_level = request.POST.get('design_water_level', '')
    historical_flood_level = request.POST.get('historical_flood_level', '')
    param_tower_basis_material = request.POST.get('param_tower_basis_material', 0)
    param_tower_base_form = request.POST.get('param_tower_base_form', 0)
    try:
        # 数据校验
        # name不可重复
        valid = MySQLDB.SelectMySql(
            "select 1 from t_bridge_base where status = 1 and bridge_name = '%s';" % bridge_name)
        if len(valid) > 0:
            message = u"桥梁名称重复,请重新输入"
        valid = MySQLDB.SelectMySql(
            "select 1 from t_bridge_base where status = 1 and bridge_code = '%s';" % bridge_code)
        if len(valid) > 0:
            message = u"桥梁编码重复,请重新输入"
        if message == "":
            if build_date == "":
                insertcmd = "INSERT INTO t_bridge_base (`bridge_code`,`bridge_name`,`line_id`,`line_name`,`section_id`,`section_name`,`pile_no`,`maintenance_depart`,`param_deck_pavement`,`length`,`under_height`,`angle`,`param_left_facilities`,`param_right_facilities`,`span_total`,`param_pier_material`,`param_pier_style`,`param_pier_base_form`,`param_abutment_material`,`param_abutment_style`,`param_abutment_base_form`,`param_expansion_type`,`expansion_position`,`param_support_type`,`remark`,`status`,`param_line_level`,`left_facilities_width`,`right_facilities_width`,`central_isolation_belt`,`driveway_width`,`approach_width`,`approach_road_width`,`approach_linear`,`under_cross_road`,`under_cross_road_no`,`param_func_type`,`param_design_load`,`slope_angle`,`build_depart`,`design_depart`,`construction_depart`,`longitude`,`latitude`,`run_load`,`bridge_total_width`,param_bridge_category,bridge_longitudinal_slope,bridge_cross_slope,deck_elevation,param_tower_structure_form,param_tower_material,bridge_clear_height,earthquake_pac,abutment_slope_protection,pier_body,modulation_structure,water_level,design_water_level,historical_flood_level,param_tower_basis_material,param_tower_base_form)VALUES('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s');" % (bridge_code, bridge_name, line_id, line_name, section_id, section_name, pile_no, maintenance_depart,param_deck_pavement, length, under_height, angle, param_left_facilities, param_right_facilities,span_total, param_pier_material, param_pier_style,param_pier_base_form, param_abutment_material, param_abutment_style, param_abutment_base_form, param_expansion_type, expansion_position, param_support_type, remark, 1, param_line_level, left_facilities_width, right_facilities_width, central_isolation_belt, driveway_width, approach_width, approach_road_width, approach_linear, under_cross_road, under_cross_road_no, param_func_type, param_design_load, slope_angle, build_depart, design_depart, construction_depart, longitude, latitude, run_load, bridge_total_width, bridgecategory, bridge_longitudinal_slope, bridge_cross_slope, deck_elevation, param_tower_structure_form, param_tower_material, bridge_clear_height, earthquake_pac, abutment_slope_protection, pier_body, modulation_structure, water_level, design_water_level, historical_flood_level, param_tower_basis_material, param_tower_base_form)
                # row = MySQLDB.CommitMySql(insertCmd)
                # if row == "ok":
                #     status = 1
                #     message = u"新增桥梁数据成功"
            else:
                insertcmd = "INSERT INTO t_bridge_base (`bridge_code`,`bridge_name`,`line_id`,`line_name`,`section_id`,`section_name`,`pile_no`,`maintenance_depart`,`param_deck_pavement`,`length`,`under_height`,`angle`,`param_left_facilities`,`param_right_facilities`,`span_total`,`param_pier_material`,`param_pier_style`,`param_pier_base_form`,`param_abutment_material`,`param_abutment_style`,`param_abutment_base_form`,`param_expansion_type`,`expansion_position`,`param_support_type`,`remark`,`status`,`build_date`,`param_line_level`,`left_facilities_width`,`right_facilities_width`,`central_isolation_belt`,`driveway_width`,`approach_width`,`approach_road_width`,`approach_linear`,`under_cross_road`,`under_cross_road_no`,`param_func_type`,`param_design_load`,`slope_angle`,`build_depart`,`design_depart`,`construction_depart`,`longitude`,`latitude`,`run_load`,`bridge_total_width`,param_bridge_category,bridge_longitudinal_slope, bridge_cross_slope, deck_elevation, param_tower_structure_form, param_tower_material, bridge_clear_height, earthquake_pac, abutment_slope_protection, pier_body, modulation_structure, water_level, design_water_level, historical_flood_level, param_tower_basis_material, param_tower_base_form)VALUES('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s');" % (
                    bridge_code, bridge_name, line_id, line_name, section_id, section_name, pile_no, maintenance_depart,
                    param_deck_pavement, length, under_height, angle, param_left_facilities, param_right_facilities,
                    span_total, param_pier_material, param_pier_style,
                    param_pier_base_form, param_abutment_material, param_abutment_style, param_abutment_base_form,
                    param_expansion_type, expansion_position, param_support_type, remark, 1, build_date,
                    param_line_level, left_facilities_width, right_facilities_width, central_isolation_belt,
                    driveway_width, approach_width, approach_road_width, approach_linear, under_cross_road,
                    under_cross_road_no, param_func_type, param_design_load, slope_angle,
                    build_depart, design_depart, construction_depart, longitude, latitude, run_load, bridge_total_width,
                    bridgecategory, bridge_longitudinal_slope, bridge_cross_slope, deck_elevation, param_tower_structure_form, param_tower_material, bridge_clear_height, earthquake_pac, abutment_slope_protection, pier_body, modulation_structure, water_level, design_water_level, historical_flood_level, param_tower_basis_material, param_tower_base_form)
            row1 = MySQLDB.CommitMySql(insertcmd)
            if row1 == "ok":
                status = 1
                message = u"新增桥梁数据成功"
        data = {
            "status": status,
            "message": message,
            "data": []
        }
    except Exception as e:
        data = {
            "status": 0,
            "message": u"新增桥梁数据失败",
            "data": [
                "INSERT INTO t_bridge_base (`bridge_code`,`bridge_name`,`line_id`,`line_name`,`section_id`,`section_name`,`pile_no`,`maintenance_depart`,`param_deck_pavement`,`length`,`under_height`,`angle`,`param_left_facilities`,`param_right_facilities`,`span_total`,`param_pier_material`,`param_pier_style`,`param_pier_base_form`,`param_abutment_material`,`param_abutment_style`,`param_abutment_base_form`,`param_expansion_type`,`expansion_position`,`param_support_type`,`remark`,`status`,`build_date`,`param_line_level`,`left_facilities_width`,`right_facilities_width`,`central_isolation_belt`,`driveway_width`,`approach_width`,`approach_road_width`,`approach_linear`,`under_cross_road`,`under_cross_road_no`,`param_func_type`,`param_design_load`,`slope_angle`,`build_depart`,`design_depart`,`construction_depart`,`longitude`,`latitude`,`run_load`,`bridge_total_width`,param_bridge_category,bridge_longitudinal_slope, bridge_cross_slope, deck_elevation, param_tower_structure_form, param_tower_material, bridge_clear_height, earthquake_pac, abutment_slope_protection, pier_body, modulation_structure, water_level, design_water_level, historical_flood_level, param_tower_basis_material, param_tower_base_form)VALUES('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s');" % (
                    bridge_code, bridge_name, line_id, line_name, section_id, section_name, pile_no, maintenance_depart,
                    param_deck_pavement, length, under_height, angle, param_left_facilities, param_right_facilities,
                    span_total, param_pier_material, param_pier_style,
                    param_pier_base_form, param_abutment_material, param_abutment_style, param_abutment_base_form,
                    param_expansion_type, expansion_position, param_support_type, remark, 1, build_date,
                    param_line_level, left_facilities_width, right_facilities_width, central_isolation_belt,
                    driveway_width, approach_width, approach_road_width, approach_linear, under_cross_road,
                    under_cross_road_no, param_func_type, param_design_load, slope_angle,
                    build_depart, design_depart, construction_depart, longitude, latitude, run_load, bridge_total_width,
                    bridgecategory, bridge_longitudinal_slope, bridge_cross_slope, deck_elevation, param_tower_structure_form, param_tower_material, bridge_clear_height, earthquake_pac, abutment_slope_protection, pier_body, modulation_structure, water_level, design_water_level, historical_flood_level, param_tower_basis_material, param_tower_base_form)]
        }
    return JsonResponse(data)


# 功能：桥梁列表基本信息
# 传入参数：request
# 返回参数：response
@auth
def QLXJ_QLLB_basic(request):
    bridge_id = request.GET["bridge_id"]
    line = []  # 路线
    section = []  # 路段
    lineLevel = []  # 路线等级
    funcType = []  # 功能类型
    designLoad = []  # 设计载荷
    approachAlignment = [{"name": u"直线","id":"1"}, {"name": u"曲线","id":"2"}]  # 引道线形
    supportType = []  # 支座类型
    pierStyle = []  # 桥墩形式
    material = []  # 桥墩、桥台材料
    form = []  # 桥墩、桥台
    abutmentStyle = []  # 桥台形式
    deckPavement = []  # 桥面铺装
    expJoint = []  # 伸缩缝类型
    facilities = []  # 左\右侧防护设施
    bridgetype = []  # 桥型
    deckboardposition = []  # 桥面板位
    upperForceForm = []  # 上部结构受力形式
    upperStructureForm = []  # 上部结构形式

    towerBasisMaterial = []  # 桥塔基础材料
    towerStructureForm = []  # 桥塔结构形式
    towerMaterial = []  # 桥塔材料
    upperMaterial = [] # 上部结构材料
    try:
        line = Common.GetLineInfoList()
        section = Common.GetSectionInfoList()
        lineLevel = Common.GetParamInfoByCode("lineLevel")  # 路线等级
        funcType = Common.GetParamInfoByCode("funcType")  # 功能类型
        designLoad = Common.GetParamInfoByCode("designLoad")  # 设计载荷
        supportType = Common.GetParamInfoByCode("supportType")  # 支座类型:
        pierStyle = Common.GetParamInfoByCode("pierStyle")  # 桥墩形式
        material = Common.GetParamInfoByCode("material")  # 桥墩、桥台材料
        form = Common.GetParamInfoByCode("form")  # 桥墩、桥台基础形式
        abutmentStyle = Common.GetParamInfoByCode("style")  # 桥台形式
        deckPavement = Common.GetParamInfoByCode("deckPavement")  # 桥面铺装
        expJoint = Common.GetParamInfoByCode("expJoint")  # 伸缩缝类型
        facilities = Common.GetParamInfoByCode("facilities")  # 左\右侧防护设施
        bridgecategory = Common.GetParamInfoByCode("bridgeCategory")  # 桥梁类型
        bridgetype = Common.GetParamInfoByCode("bridgetype")  # 桥型
        deckboardposition = Common.GetParamInfoByCode("deckboardposition")  # 桥面板位
        upperForceForm = Common.GetParamInfoByCode("upperForceForm")  # 上部结构受力形式
        upperStructureForm = Common.GetParamInfoByCode("upperStructureForm")  # 上部结构形式
        towerBasisMaterial = Common.GetParamInfoByCode("basisMaterial")  # 桥塔基础材料
        towerStructureForm = Common.GetParamInfoByCode("towerStructureForm")  # 桥塔结构形式
        towerMaterial = Common.GetParamInfoByCode("towerMaterial")  # 桥塔材料
        upperMaterial = Common.GetParamInfoByCode("upperMaterial")

    except Exception as e:
        pass
    uplist = []
    try:
        # 获取上部结构信息
        row = MySQLDB.SelectMySql(
            "select upperpart_id,upper_order,span_number,span_arrangement,(select param_name from t_sys_param where param_id = param_deck_board_position) param_deck_board_position,(select param_name from t_sys_param where param_id = param_force_form) param_force_form,(select param_name from t_sys_param where param_id = param_form) param_form,(select param_name from t_sys_param where param_id = param_material) param_material,(select param_name from t_sys_param where param_id = param_bridge_type) param_bridge_type  ,param_bridge_type as param_bridge_type_id from t_bridge_upperpart where bridge_id = %s;" % bridge_id)
        if len(row) > 0:
            key = ["upperpart_id", "upper_order", "span_number", "span_arrangement", "param_deck_board_position",
                   "param_force_form", "param_form", "param_material", "param_bridge_type", "param_bridge_type_id"]
            for i in range(len(row)):
                info = {}
                for j in range(len(key)):
                    info[key[j]] = row[i][j]
                uplist.append(info)
    except Exception as e:
        uplist = []
    xlist = []
    try:
        # if 1==1:
        # 获取桥梁列表基本信息
        # aa=""
        row1 = MySQLDB.SelectMySql(
            "select bridge_id,`bridge_code`,`bridge_name`,`line_id`,`line_name`,`section_id`,`section_name`,`pile_no`,`maintenance_depart`,param_deck_pavement,`length`,`under_height`,`angle`,param_left_facilities,param_right_facilities,`span_total`,param_pier_material, param_pier_style, param_pier_base_form, param_abutment_material,param_abutment_style,param_abutment_base_form,param_expansion_type,`expansion_position`,param_support_type,`remark`,`status`,date_format(build_date,'%%Y-%%m-%%d') as build_date , param_line_level,`left_facilities_width`,`right_facilities_width`,`central_isolation_belt`,`driveway_width`,`approach_width`,`approach_road_width`,`approach_linear`,`under_cross_road`,`under_cross_road_no`, param_func_type,param_design_load,`slope_angle`,`build_depart`,`design_depart`,`construction_depart`,`longitude`,`latitude`,`run_load`,`bridge_total_width`,`param_bridge_category`,bridge_longitudinal_slope, bridge_cross_slope, deck_elevation, ifnull(param_tower_structure_form,0), ifnull(param_tower_material,0), bridge_clear_height, earthquake_pac, abutment_slope_protection, pier_body, modulation_structure, water_level, design_water_level, historical_flood_level, ifnull(param_tower_basis_material,0), ifnull(param_tower_base_form,0) from t_bridge_base where bridge_id=%s;" % bridge_id)
        if len(row1) > 0:
            key = ['bridge_id', 'bridge_code', 'bridge_name', 'line_id', 'line_name', 'section_id', 'section_name', 'pile_no', 'maintenance_depart', 'param_deck_pavement', 'length', 'under_height', 'angle', 'param_left_facilities', 'param_right_facilities', 'span_total', 'param_pier_material', 'param_pier_style', 'param_pier_base_form', 'param_abutment_material', 'param_abutment_style', 'param_abutment_base_form', 'param_expansion_type', 'expansion_position', 'param_support_type', 'remark', 'status', 'build_date', 'param_line_level', 'left_facilities_width', 'right_facilities_width', 'central_isolation_belt', 'driveway_width', 'approach_width', 'approach_road_width', 'approach_linear', 'under_cross_road', 'under_cross_road_no', 'param_func_type', 'param_design_load', 'slope_angle', 'build_depart', 'design_depart', 'construction_depart', 'longitude', 'latitude', 'run_load', 'bridge_total_width', 'param_bridge_category', 'bridge_longitudinal_slope', 'bridge_cross_slope', 'deck_elevation', 'param_tower_structure_form', 'param_tower_material', 'bridge_clear_height', 'earthquake_pac', 'abutment_slope_protection', 'pier_body', 'modulation_structure', 'water_level', 'design_water_level', 'historical_flood_level', 'param_tower_basis_material', 'param_tower_base_form']
            key_int = ["length", "under_height", "angle", "left_facilities_width",
                       "right_facilities_width", "central_isolation_belt", "driveway_width", "approach_width",
                       "approach_road_width"]
            for i in range(len(row1)):
                info = {}
                for j in range(len(key)):
                    # aa = key[j]
                    if key[j] in key_int:
                        info[key[j]] = str(row1[i][j])
                    elif key[j] == "param_support_type" or key[j] == "bridgecategory" or key[j] == "param_tower_structure_form" or key[j] == "param_tower_material" or key[j] == "param_tower_basis_material" or key[j] == "param_tower_base_form":
                        info[key[j]] = int(row1[i][j])
                    elif row1[i][j] == None:
                        info[key[j]] = ""
                    else:
                        info[key[j]] = row1[i][j]
                xlist.append(info)
    except Exception as e:
        xlist = []
    return render(request, "QLGL/QLSJ/QLGL_QLSJ_QLLB_JBXX.html",
                  {"line": line, "section": section, "lineLevel": lineLevel, "funcType": funcType,
                   "designLoad": designLoad, "approachAlignment": approachAlignment, "supportType": supportType,
                   "pierStyle": pierStyle, "material": material, "form": form, "abutmentStyle": abutmentStyle,
                   "deckPavement": deckPavement, "expJoint": expJoint, "facilities": facilities,
                   "bridgecategory": bridgecategory, "xlist": xlist, "bridgetype": bridgetype,
                   "deckboardposition": deckboardposition, "upperForceForm": upperForceForm,
                   "upperStructureForm": upperStructureForm, "towerBasisMaterial": towerBasisMaterial, "towerStructureForm": towerStructureForm, "towerMaterial": towerMaterial,"upperMaterial": upperMaterial, "uplist": uplist})


# 功能：桥梁列表基本信息保存
# 传入参数：request
# 返回参数：response
@authjson
def QLXJ_QLLB_basicSave(request):
    bridge_code = request.POST.get('bridge_code', '')
    bridge_name = request.POST.get('bridge_name', '')
    line_id = request.POST.get('line_id', '')
    if line_id.strip() == "":
        line_id = 0
    line_name = request.POST.get('line_name', '')
    section_id = request.POST.get('section_id', '')
    if section_id.strip() == "":
        section_id = 0
    section_name = request.POST.get('section_name', '')
    pile_no = request.POST.get('pile_no', '')
    maintenance_depart = request.POST.get('maintenance_depart', '')
    param_deck_pavement = request.POST.get('param_deck_pavement', '')
    if param_deck_pavement.strip() == "":
        param_deck_pavement = 0
    length = request.POST.get('length', 0)
    under_height = request.POST.get('under_height', 0)
    angle = request.POST.get('angle', 0)
    param_left_facilities = request.POST.get('param_left_facilities', '')
    if param_left_facilities.strip() == "":
        param_left_facilities = 0
    param_right_facilities = request.POST.get('param_right_facilities', '')
    if param_right_facilities.strip() == "":
        param_right_facilities = 0
    span_total = request.POST.get('span_total', '')
    if span_total.strip() == "":
        span_total = 0
    # under_pier_number = request.POST.get('under_pier_number', '')
    # under_abutment_number = request.POST.get('under_abutment_number', '')
    param_pier_material = request.POST.get('param_pier_material', '')
    if param_pier_material.strip() == "":
        param_pier_material = 0
    param_pier_style = request.POST.get('param_pier_style', '')
    if param_pier_style.strip() == "":
        param_pier_style = 0
    param_pier_base_form = request.POST.get('param_pier_base_form', '')
    if param_pier_base_form.strip() == "":
        param_pier_base_form = 0
    param_abutment_material = request.POST.get('param_abutment_material', '')
    if param_abutment_material.strip() == "":
        param_abutment_material = 0
    param_abutment_style = request.POST.get('param_abutment_style', '')
    if param_abutment_style.strip() == "":
        param_abutment_style = 0
    param_abutment_base_form = request.POST.get('param_abutment_base_form', '')
    if param_abutment_base_form.strip() == "":
        param_abutment_base_form = 0
    param_expansion_type = request.POST.get('param_expansion_type', '')
    if param_expansion_type.strip() == "":
        param_expansion_type = 0
    expansion_position = request.POST.get('expansion_position', '')
    param_support_type = request.POST.get('param_support_type', '')
    remark = request.POST.get('remark', '')
    status = request.POST.get('status', 1)
    build_date = request.POST.get('build_date', '')
    param_line_level = request.POST.get('param_line_level', '')
    if param_line_level.strip() == "":
        param_line_level = 0
    # deck_width = request.POST.get('deck_width', 0)
    left_facilities_width = request.POST.get('left_facilities_width', 0)
    right_facilities_width = request.POST.get('right_facilities_width', 0)
    central_isolation_belt = request.POST.get('central_isolation_belt', 0)
    driveway_width = request.POST.get('driveway_width', '0')
    approach_width = request.POST.get('approach_width', '0')
    approach_road_width = request.POST.get('approach_road_width', '0')
    approach_linear = request.POST.get('approach_linear', '')
    under_cross_road = request.POST.get('under_cross_road', '')
    under_cross_road_no = request.POST.get('under_cross_road_no', '')
    # cross_situation_explain = request.POST.get('cross_situation_explain', '')
    param_func_type = request.POST.get('param_func_type', '')
    if param_func_type.strip() == "":
        param_func_type = 0
    param_design_load = request.POST.get('param_design_load', '')
    slope_angle = request.POST.get('slope_angle', '')
    build_depart = request.POST.get('build_depart', '')
    design_depart = request.POST.get('design_depart', '')
    construction_depart = request.POST.get('construction_depart', '')
    longitude = request.POST.get('longitude', '')
    latitude = request.POST.get('latitude', '')
    run_load = request.POST.get('run_load', '')
    bridge_total_width = request.POST.get('bridge_total_width', '')
    bridgecategory = request.POST.get('bridgecategory', '')
    bridge_id = request.POST.get('bridge_id', '')
    status = 0
    message = ""

    # 2017-8-25 修改，增加参数
    bridge_longitudinal_slope = request.POST.get('bridge_longitudinal_slope', '')
    bridge_cross_slope = request.POST.get('bridge_cross_slope', '')
    deck_elevation = request.POST.get('deck_elevation', '')
    param_tower_structure_form = request.POST.get('param_tower_structure_form', 0)
    param_tower_material = request.POST.get('param_tower_material', 0)
    bridge_clear_height = request.POST.get('bridge_clear_height', '')
    earthquake_pac = request.POST.get('earthquake_pac', '')
    abutment_slope_protection = request.POST.get('abutment_slope_protection', '')
    pier_body = request.POST.get('pier_body', '')
    modulation_structure = request.POST.get('modulation_structure', '')
    water_level = request.POST.get('water_level', '')
    design_water_level = request.POST.get('design_water_level', '')
    historical_flood_level = request.POST.get('historical_flood_level', '')
    param_tower_basis_material = request.POST.get('param_tower_basis_material', 0)
    param_tower_base_form = request.POST.get('param_tower_base_form', 0)
    try:
        # 数据校验
        # name不可重复
        valid = MySQLDB.SelectMySql(
            "select 1 from t_bridge_base where status = 1 and bridge_name = '%s' and bridge_id <> %s;" % (
            bridge_name, bridge_id))
        if len(valid) > 0:
            message = u"桥梁名称重复,请重新输入"
        valid = MySQLDB.SelectMySql(
            "select 1 from t_bridge_base where status = 1 and bridge_code = '%s' and bridge_id <> %s;" % (
            bridge_code, bridge_id))
        if len(valid) > 0:
            message = u"路段名称重复,请重新输入"
        if message == "":
            if build_date == "":
                row = MySQLDB.CommitMySql(
                    "UPDATE t_bridge_base SET `bridge_code`= '%s',`bridge_name`= '%s',`line_id`= '%s',`line_name`='%s',`section_id`='%s',`section_name`='%s',`pile_no`='%s',`maintenance_depart`='%s',`param_deck_pavement`='%s',`length`='%s',`under_height`='%s',`angle`='%s',`param_left_facilities`='%s',`param_right_facilities`='%s',`span_total`='%s',`param_pier_material`='%s',`param_pier_style`='%s',`param_pier_base_form`='%s',`param_abutment_material`='%s',`param_abutment_style`='%s',`param_abutment_base_form`='%s',`param_expansion_type`='%s',`expansion_position`='%s',`param_support_type`='%s',`remark`='%s',`status`='%s',`param_line_level`='%s',`left_facilities_width`='%s',`right_facilities_width`='%s',`central_isolation_belt`='%s',`driveway_width`='%s',`approach_width`='%s',`approach_road_width`='%s',`approach_linear`='%s',`under_cross_road`='%s',`under_cross_road_no`='%s',`param_func_type`='%s',`param_design_load`='%s',`slope_angle`='%s',`build_depart`='%s',`design_depart`='%s',`construction_depart`='%s',`longitude`='%s',`latitude`='%s',`run_load`='%s',`bridge_total_width`='%s',param_bridge_category='%s', bridge_longitudinal_slope = '%s', bridge_cross_slope = '%s', deck_elevation = '%s', param_tower_structure_form = '%s', param_tower_material = '%s', bridge_clear_height = '%s', earthquake_pac = '%s', abutment_slope_protection = '%s', pier_body = '%s', modulation_structure = '%s', water_level = '%s', design_water_level = '%s', historical_flood_level = '%s', param_tower_basis_material = '%s', param_tower_base_form = '%s' WHERE bridge_id = %s;" % (
                    bridge_code, bridge_name, line_id, line_name, section_id, section_name, pile_no, maintenance_depart,
                    param_deck_pavement, length, under_height, angle, param_left_facilities, param_right_facilities,
                    span_total, param_pier_material, param_pier_style,
                    param_pier_base_form, param_abutment_material, param_abutment_style, param_abutment_base_form,
                    param_expansion_type, expansion_position, param_support_type, remark, 1, param_line_level,
                    left_facilities_width, right_facilities_width, central_isolation_belt, driveway_width,
                    approach_width, approach_road_width, approach_linear, under_cross_road, under_cross_road_no,
                    param_func_type, param_design_load, slope_angle, build_depart,
                    design_depart, construction_depart, longitude, latitude, run_load, bridge_total_width,
                    bridgecategory, bridge_longitudinal_slope, bridge_cross_slope, deck_elevation, param_tower_structure_form, param_tower_material, bridge_clear_height, earthquake_pac, abutment_slope_protection, pier_body, modulation_structure, water_level, design_water_level, historical_flood_level, param_tower_basis_material, param_tower_base_form, bridge_id))
                if row == "ok":
                    status = 1
                    message = u"桥梁基本信息保存成功"
            else:
                row1 = MySQLDB.CommitMySql(
                    "UPDATE t_bridge_base SET `bridge_code`= '%s',`bridge_name`= '%s',`line_id`= '%s',`line_name`='%s',`section_id`='%s',`section_name`='%s',`pile_no`='%s',`maintenance_depart`='%s',`param_deck_pavement`='%s',`length`='%s',`under_height`='%s',`angle`='%s',`param_left_facilities`='%s',`param_right_facilities`='%s',`span_total`='%s',`param_pier_material`='%s',`param_pier_style`='%s',`param_pier_base_form`='%s',`param_abutment_material`='%s',`param_abutment_style`='%s',`param_abutment_base_form`='%s',`param_expansion_type`='%s',`expansion_position`='%s',`param_support_type`='%s',`remark`='%s',`status`='%s',`build_date`='%s',`param_line_level`='%s',`left_facilities_width`='%s',`right_facilities_width`='%s',`central_isolation_belt`='%s',`driveway_width`='%s',`approach_width`='%s',`approach_road_width`='%s',`approach_linear`='%s',`under_cross_road`='%s',`under_cross_road_no`='%s',`param_func_type`='%s',`param_design_load`='%s',`slope_angle`='%s',`build_depart`='%s',`design_depart`='%s',`construction_depart`='%s',`longitude`='%s',`latitude`='%s',`run_load`='%s',`bridge_total_width`='%s',param_bridge_category='%s', bridge_longitudinal_slope = '%s', bridge_cross_slope = '%s', deck_elevation = '%s', param_tower_structure_form = '%s', param_tower_material = '%s', bridge_clear_height = '%s', earthquake_pac = '%s', abutment_slope_protection = '%s', pier_body = '%s', modulation_structure = '%s', water_level = '%s', design_water_level = '%s', historical_flood_level = '%s', param_tower_basis_material = '%s', param_tower_base_form = '%s' WHERE bridge_id = %s;" % (
                    bridge_code, bridge_name, line_id, line_name, section_id, section_name, pile_no, maintenance_depart,
                    param_deck_pavement, length, under_height, angle, param_left_facilities, param_right_facilities,
                    span_total, param_pier_material, param_pier_style,
                    param_pier_base_form, param_abutment_material, param_abutment_style, param_abutment_base_form,
                    param_expansion_type, expansion_position, param_support_type, remark, 1, build_date,
                    param_line_level, left_facilities_width, right_facilities_width, central_isolation_belt,
                    driveway_width, approach_width, approach_road_width, approach_linear, under_cross_road,
                    under_cross_road_no, param_func_type, param_design_load, slope_angle,
                    build_depart, design_depart, construction_depart, longitude, latitude, run_load, bridge_total_width,
                    bridgecategory, bridge_longitudinal_slope, bridge_cross_slope, deck_elevation, param_tower_structure_form, param_tower_material, bridge_clear_height, earthquake_pac, abutment_slope_protection, pier_body, modulation_structure, water_level, design_water_level, historical_flood_level, param_tower_basis_material, param_tower_base_form, bridge_id))
                if row1 == "ok":
                    status = 1
                    message = u"桥梁基本信息保存成功"
        data = {
            "status": status,
            "message": message,
            "data": []
        }
    except Exception as e:
        data = {
            "status": 0,
            "message": str(e),
            "data": []
        }
    return JsonResponse(data)


# 功能：新增上部结构保存
# 传入参数：request
# 返回参数：response
@authjson
def QLXJ_QLLB_UpperSave(request):
    bridge_id = request.POST.get('bridge_id', '')
    param_deck_board_position = request.POST.get('param_deck_board_position', '')
    param_force_form = request.POST.get('param_force_form', '')
    param_form = request.POST.get('param_form', '')
    span_arrangement = request.POST.get('span_arrangement', '')
    span_number = request.POST.get('span_number', '')
    param_material = request.POST.get('param_material', '')
    param_bridge_type = request.POST.get('param_bridge_type', '')
    upper_order = request.POST.get('upper_order', '')

    try:
        insertCmd = "INSERT INTO t_bridge_upperpart (`bridge_id`,`param_deck_board_position`,`param_force_form`,`param_form`,`span_arrangement`,`span_number`,`param_material`,`param_bridge_type`,`upper_order`) VALUES('%s','%s','%s','%s','%s','%s','%s','%s','%s');" % (
            bridge_id, param_deck_board_position, param_force_form, param_form, span_arrangement, span_number,
            param_material, param_bridge_type, upper_order)
        row = MySQLDB.CommitMySql(insertCmd)
        uplist = []
        try:
            # 获取上部结构信息
            row = MySQLDB.SelectMySql(
                "select upperpart_id,upper_order,span_number,span_arrangement,(select param_name from t_sys_param where param_id = param_deck_board_position) param_deck_board_position,(select param_name from t_sys_param where param_id = param_force_form) param_force_form,(select param_name from t_sys_param where param_id = param_form) param_form,(select param_name from t_sys_param where param_id = param_material) param_material,(select param_name from t_sys_param where param_id = param_bridge_type) param_bridge_type ,param_bridge_type as param_bridge_type_id from t_bridge_upperpart where bridge_id = %s;" % bridge_id)
            if len(row) > 0:
                key = ["upperpart_id", "upper_order", "span_number", "span_arrangement", "param_deck_board_position",
                       "param_force_form", "param_form", "param_material", "param_bridge_type", "param_bridge_type_id"]
                for i in range(len(row)):
                    info = {}
                    for j in range(len(key)):
                        info[key[j]] = row[i][j]
                    uplist.append(info)
        except Exception as e:
            uplist = []
        data = {
            "status": 1,
            "message": u"新增上部结构数据成功",
            "data": uplist
        }
    except Exception as e:
        data = {
            "status": 0,
            "message": u"新增上部结构数据失败",
            "data": []
        }
    return JsonResponse(data)


# 功能：上部结构删除
# 传入参数：request
# 返回参数：response
@authjson
def QLXJ_QLLB_UpperDel(request):
    upperpart_id = request.GET["upperpart_id"]
    try:
        row = MySQLDB.CommitMySql("DELETE FROM t_bridge_upperpart WHERE upperpart_id = '%s' ;" % (upperpart_id))
        data = {
            "status": 1,
            "message": u"del上部结构数据成功",
            "data": []
        }
    except Exception as e:
        data = {
            "status": 0,
            "message": u"del上部结构数据失败",
            "data": []
        }
    return JsonResponse(data)


# 功能：桥梁列表构件信息页面
# 传入参数：request
# 返回参数：response
@auth
def QLXJ_QLLB_GJXX_index(request):
    bridge_id = request.GET["bridge_id"]
    datalist = []
    try:
        # 获取部件信息
        row = MySQLDB.SelectMySql(
            "select parts_no,parts_code,bridge_parts_name,position_code,param_bridge_type_code from t_sys_bridge_parts where exists(select 1 from (select param_code from t_sys_param a where param_id in (select distinct(param_bridge_type) from t_bridge_upperpart where bridge_id = %s group by param_bridge_type) group by a.param_id limit 1) t where t.param_code = param_bridge_type_code);" % bridge_id)
        if len(row) > 0:
            key = ["parts_no", "parts_code", "bridge_parts_name", "position_code", "param_bridge_type_code"]
            for i in range(len(row)):
                info = {}
                for j in range(len(key)):
                    info[key[j]] = row[i][j]
                    if key[j] == "parts_code":
                        row1 = MySQLDB.SelectMySql(
                            "select count(1) from t_bridge_member where bridge_id='%s' and parts_code='%s' and status=1;" % (
                            bridge_id, info[key[j]]))
                        info["counts"] = row1[0][0]
                datalist.append(info)

        data = {"status": 1,
                "message": u"查询构件列表数据成功",
                "data": datalist
                }
    except Exception as e:
        data = {
            "status": 0,
            "message": u"查询构件列表数据失败",
            "data": []
        }
    return render(request, "QLGL/QLSJ/QLGL_QLSJ_QLLB_GJXX.html", {"bridge_id": bridge_id, "data": data})


# 功能：桥梁列表构件信息页面跳转
# 传入参数：request
# 返回参数：response
@auth
def QLXJ_QLLB_GJXX_data(request):
    bridge_id = request.GET["bridge_id"]
    parts_code = request.GET["parts_code"]
    param_bridge_type_code = request.GET["param_bridge_type_code"]
    position_code = request.GET["position_code"]
    return render(request, "QLGL/QLSJ/QLGL_QLSJ_QLLB_GJXX_SEE.html",
                  {"bridge_id": json.dumps(bridge_id), "parts_code": json.dumps(parts_code),
                   "param_bridge_type_code": json.dumps(param_bridge_type_code),"position_code": json.dumps(position_code)})


# 功能：桥梁列表构件信息详细数据
# 传入参数：request
# 返回参数：response
@authjson
def QLXJ_QLLB_GJXX_detail(request):
    bridge_id = request.GET["bridge_id"]
    parts_code = request.GET["parts_code"]
    datalist = []
    try:
        # 获取构件信息
        row = MySQLDB.SelectMySql(
            "SELECT b.parts_name,a.bridge_member_id,a.member_name,a.notes from t_bridge_member a inner join t_sys_parts b on a.parts_code = b.parts_code and b.status = 1 where a.bridge_id=%s and a.parts_code=%s and a.status = 1;" % (
            bridge_id, parts_code))
        if len(row) > 0:
            key = ["parts_name", "bridge_member_id", "member_name", "notes"]
            for i in range(len(row)):
                info = {}
                for j in range(len(key)):
                    info[key[j]] = row[i][j]
                datalist.append(info)
        data = {
            "status": 1,
            "message": u"查询构件列表数据成功",
            "data": datalist
        }
    except Exception as e:
        data = {
            "status": 0,
            "message": u"查询构件列表数据失败",
            "data": []
        }
    return JsonResponse(data)


# 功能：删除桥梁列表构件
# 传入参数：request
# 返回参数：response
@authjson
def QLXJ_QLLB_GJXX_detail_del(request):
    bridge_member_id = request.GET["bridge_member_id"]
    try:
        temp = bridge_member_id.split('|')
        # count = len(temp)
        # del temp[count - 1]
        for i in temp:
            row = MySQLDB.CommitMySql("UPDATE t_bridge_member SET status = 0 WHERE bridge_member_id = '%s';" % i)
        data = {
            "status": 1,
            "message": u"del成功",
            "data": []
        }
    except Exception as e:
        data = {
            "status": 0,
            "message": u"del失败",
            "data": []
        }
    return JsonResponse(data)


# 功能：桥梁列表构件新增
# 传入参数：request
# 返回参数：response
@auth
def QLXJ_QLLB_GJXX_add(request):
    """
    桥梁列表构件新增
    """
    parts_code = request.GET["parts_code"]
    bridge_id = request.GET["bridge_id"]
    param_bridge_type_code = request.GET["param_bridge_type_code"]
    position_code = request.GET["position_code"]
    position = [{"key": "L", "name": u"左幅"}, {"key": "R", "name": u"右幅"}, {"key": "NONE", "name": u"无左右幅"}]
    # try:
    #     partstype = Common.PartstypeInfoList("parts_code")  # 构件类型
    # except Exception as e:
    #     pass
    partstype = []
    try:
        # 新增验证，当为斜拉索桥时，上部结构和下部结构中，位置参数不需要左右幅
        if ("bridgeType-9" == param_bridge_type_code and 2 != int(position_code)):
            position = [{"key": "NONE", "name": u"无左右幅"}]

        row = MySQLDB.SelectMySql("select parts_code,parts_name from t_sys_parts where parts_code =%s;" % parts_code)
        if len(row) > 0:
            for i in range(len(row)):
                info = {}
                info["id"] = row[i][0]
                info["name"] = row[i][1]
                partstype.append(info)
    except Exception as e:
        partstype = []
    datalist = []
    try:
        # 获取子部件构件信息
        row = MySQLDB.SelectMySql(
            "SELECT parts_sub_id,name from t_sys_parts_sub where param_bridge_type_code = '%s' and parts_code = '%s' and status = 1;" % (
            param_bridge_type_code, parts_code))
        if len(row) > 0:
            key = ["parts_sub_id", "name"]
            for i in range(len(row)):
                info = {}
                for j in range(len(key)):
                    info[key[j]] = row[i][j]
                datalist.append(info)
    except Exception as e:
        pass
    return render(request, "QLGL/QLSJ/QLGL_QLSJ_QLLB_GJXX_ADD.html",
                  {"position": position, "partstype": partstype, "datalist": datalist,"bridge_id": json.dumps(bridge_id), "parts_code": json.dumps(parts_code)})


# 功能：查询子部件参数信息
# 传入参数：request
# 返回参数：response
@authjson
def QLXJ_QLLB_GJXX_suParter(request):
    parts_sub_id = request.GET["parts_sub_id"]
    datalist = []
    try:
        # 获取构件信息
        row = MySQLDB.SelectMySql(
            "SELECT parts_sub_rule_id,parts_sub_id,param_num,parts_num,parts_note ,position from t_sys_parts_sub_rule where parts_sub_id = '%s' and status = 1;" % (
            parts_sub_id))
        if len(row) > 0:
            key = ["parts_sub_rule_id", "parts_sub_id", "param_num", "parts_num", "parts_note"]
            for i in range(len(row)):
                info = {}
                for j in range(len(key)):
                    info[key[j]] = row[i][j]
                if row[i][5] == 1:
                    info["position"] = [{"key": "L", "name": u"左幅"}, {"key": "R", "name": u"右幅"}, {"key": "NONE", "name": u"无左右幅"}]
                elif row[i][5] == 2:
                    info["position"] = [{"key": "L", "name": u"左侧"}, {"key": "R", "name": u"右侧"}, {"key": "NONE", "name": u"无左右侧"}]
                elif row[i][5] == 3:
                    info["position"] = [{"key": "NONE", "name": u"无左右幅"}]

                datalist.append(info)
        data = {
            "status": 1,
            "message": u"查询子部件参数信息成功",
            "data": datalist
                }
    except Exception as e:
        data = {
            "status": 0,
            "message": u"查询子部件参数信息失败",
            "data": []
        }
    return JsonResponse(data)


# 功能：新增子部件参数信息
# 传入参数：request
# 返回参数：response
@authjson
def QLXJ_QLLB_GJXX_suParterSave(request):
    bridge_id = request.POST.get('bridge_id', '')
    parts_code = request.POST.get('parts_code', '')
    create_date = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    # return HttpResponse(create_date)
    try:
        message = u"新增子部件规则失败"
        sub_part_id = request.POST.get('sub_part_id', 0)
        parameter1 = request.POST.get('parameter1', '')
        parameter2 = request.POST.get('parameter2', '')
        parameter3 = request.POST.get('parameter3', '')
        keywords = request.POST.get('keywords', '')
        notes = request.POST.get('notes', '')
        position = request.POST.get('position', '')
        row = MySQLDB.CommitMySql("INSERT INTO `t_bridge_member_rule`(`bridge_id`,`parts_code`,`parameter1`,`parameter2`,`parameter3`,`keywords`,`notes`,`position`,`status`,create_date,parts_sub_id)VALUES('%s','%s','%s','%s','%s','%s','%s','%s',1,'%s','%s');" % (bridge_id, parts_code, parameter1, parameter2, parameter3, keywords, notes, position, create_date, sub_part_id))

        message = u"新增子部件news失败"
        # req = json.loads(request.body)
        # datas = req['all_information']
        datas = request.POST.get('all_information', '')
        data_s = eval(datas)
        keywords = request.POST.get('keywords', '')
        notes = request.POST.get('notes', '')
        parameter1 = request.POST.get('parameter1', '')
        parameter2 = request.POST.get('parameter2', '')
        parameter3 = request.POST.get('parameter3', '')
        row1 = MySQLDB.SelectMySql("SELECT bridge_member_rule_id from t_bridge_member_rule where bridge_id = '%s' and parts_code = '%s' and parameter1 = '%s'and parameter2 = '%s' and parameter3 = '%s'and keywords ='%s'and create_date = '%s'and notes = '%s' and status = 1 ;" % (bridge_id, parts_code, parameter1, parameter2, parameter3, keywords,create_date, notes))
        for i in data_s:
            row2 = MySQLDB.SelectMySql("SELECT count(1) from t_bridge_member where member_name = '%s' and bridge_id = '%s' and parts_code = '%s'and  status = 1 ;" % (i["member_name"], str(bridge_id), str(parts_code)))
            if row2[0][0] == 0:
                row = MySQLDB.CommitMySql("INSERT INTO `t_bridge_member`(`member_name`,`bridge_id`,`parts_code`,`bridge_member_rule_id`,`notes`,`status`,parts_sub_id) VALUES('%s','%s','%s','%s','%s',1,'%s');" % (i["member_name"], str(bridge_id), str(parts_code), row1[0][0], i["notes1"], sub_part_id))

        data = {
            "status": 1,
            "message": u"新增子部件news成功",
            "data": []
        }
    except Exception as e:
        data = {
            "status": 0,
            "message": message,
            "data": []
        }
    return JsonResponse(data)


# 功能：评分单元列表
# 传入参数：request
# 返回参数：response
@auth
def QLXJ_QLLB_PFDY(request):
    bridge_id = request.GET["bridge_id"]
    bridgetype = []
    try:
        bridgetype = Common.GetParamInfoByCode("bridgetype")
    except Exception as e:
        pass
    datalist = []
    try:
        #
        row = MySQLDB.SelectMySql(
            "SELECT bridge_id,unit_id,name,bridge_type_code,bridge_type from t_bridge_unit where bridge_id = '%s' and status=1;" % (
            bridge_id))
        if len(row) > 0:
            key = ["bridge_id", "unit_id", "name", "bridge_type_code", "bridge_type"]
            for i in range(len(row)):
                info = {}
                for j in range(len(key)):
                    info[key[j]] = row[i][j]
                datalist.append(info)
    except Exception as e:
        pass
    return render(request, "QLGL/QLSJ/QLGL_QLSJ_QLLB_PFDY.html", {"bridge_id":json.dumps(bridge_id)})
# 功能：评分单元列表
# 传入参数：request
# 返回参数：response
@auth
def QLXJ_QLLB_PFDY_list(request):
    bridge_id = request.GET["bridge_id"]
    bridgetype = []
    try:
        bridgetype = Common.GetParamInfoByCode("bridgetype")
    except Exception as e:
        pass
    datalist = []
    try:
        #
        row = MySQLDB.SelectMySql(
            "SELECT bridge_id,unit_id,name,bridge_type_code,bridge_type from t_bridge_unit where bridge_id = '%s' and status=1;" % (
            bridge_id))
        if len(row) > 0:
            key = ["bridge_id", "unit_id", "name", "bridge_type_code", "bridge_type"]
            for i in range(len(row)):
                info = {}
                for j in range(len(key)):
                    info[key[j]] = row[i][j]
                datalist.append(info)
        data = {
            "status": 0,
            "message": u"查询评分单元数据失败！",
            "data": datalist
        }
    except Exception as e:
        data = {
            "status": 0,
            "message": u"查询评分单元数据失败！",
            "data": []
        }

    return JsonResponse(data)

# 功能：新增评分单元页面
# 传入参数：request
# 返回参数：response
@auth
def QLXJ_QLLB_PFDY_add(request):
    bridge_id = request.GET["bridge_id"]
    bridgetype = []
    param_bridge_type = 0
    try:
        bridgetype = Common.GetParamInfoByCode("bridgetype")
        row = MySQLDB.SelectMySql("select param_bridge_type from t_bridge_upperpart where bridge_id='%s';" % (bridge_id))
        if len(row) > 0:
            param_bridge_type = int(row[0][0])
        else:
            param_bridge_type = -1
 
    except Exception as e:
        pass
    return render(request, "QLGL/QLSJ/QLGL_QLSJ_QLLB_PFDY_ADD.html", {"bridgetype": bridgetype, "bridge_id": bridge_id,"param_bridge_type":param_bridge_type})


# 功能：新增评分单元保存
# 传入参数：request
# 返回参数：response
@authjson
def QLXJ_QLLB_PFDY_addsave(request):
    bridge_id = request.POST["bridge_id"]
    name = request.POST["name"]
    bridge_type = request.POST["bridge_type"]
    param_id = request.POST["param_id"]
    try:
        row = MySQLDB.CommitMySql(
            "INSERT into t_bridge_unit(`name`,`bridge_id`,`status`,`bridge_type_code`,`bridge_type`) select '%s','%s',1,param_code,param_name from t_sys_param where param_id = '%s';" % (
            name, bridge_id, param_id))
        data = {
            "status": 1,
            "message": u"新增评分单元成功",
            "data": []
        }
    except Exception as e:
        data = {
            "status": 0,
            "message": u"新增评分单元失败",
            "data": []
        }
    return JsonResponse(data)
    # return HttpResponse(row)


# 功能：修改评分单元页面
# 传入参数：request
# 返回参数：response
@auth
def QLXJ_QLLB_PFDY_mod(request):
    bridge_id = request.GET["bridge_id"]
    unit_id = request.GET["unit_id"]
    datalist = []
    try:
        # 获取子部件构件信息
        row = MySQLDB.SelectMySql(
            "SELECT bridge_id,unit_id,name,bridge_type_code,bridge_type from t_bridge_unit where unit_id = '%s' and status=1;" % (
            unit_id))
        if len(row) > 0:
            key = ["bridge_id", "unit_id", "name", "bridge_type_code", "bridge_type"]
            for i in range(len(row)):
                info = {}
                for j in range(len(key)):
                    info[key[j]] = row[i][j]
                datalist.append(info)
    except Exception as e:
        datalist = []
    bridgetype = []
    try:
        bridgetype = Common.GetParamInfoByCode("bridgetype")
    except Exception as e:
        pass
    return render(request, "QLGL/QLSJ/QLGL_QLSJ_QLLB_PFDY_MODIFY.html",
                  {"bridgetype": bridgetype, "bridge_id": bridge_id, "unit_id": unit_id, "datalist": datalist})


# 功能：修改评分单元保存
# 传入参数：request
# 返回参数：response
@authjson
def QLXJ_QLLB_PFDY_modsave(request):
    bridge_id = request.POST["bridge_id"]
    name = request.POST["name"]
    bridge_type = request.POST["bridge_type"]
    param_id = request.POST["param_id"]
    unit_id = request.POST["unit_id"]
    try:
        row = MySQLDB.SelectMySql(
            "SELECT param_code, param_name from t_sys_param where param_id = '%s';" % (param_id))
        row1 = MySQLDB.CommitMySql(
            "UPDATE t_bridge_unit SET `name`= '%s',`bridge_id`='%s',`bridge_type_code`='%s',`bridge_type`='%s' where unit_id = '%s';" % (
            name, bridge_id, row[0][0], row[0][1],unit_id))
        data = {
            "status": 1,
            "message": u"修改评分单元成功",
            "data": []
        }
    except Exception as e:
        data = {
            "status": 0,
            "message": u"修改评分单元失败",
            "data": []
        }
    return JsonResponse(data)
    # return HttpResponse(row)


# 功能：删除评分单元保存
# 传入参数：request
# 返回参数：response
@authjson
def QLXJ_QLLB_PFDY_del(request):
    unit_id = request.GET["unit_id"]
    try:
        row1 = MySQLDB.CommitMySql("update t_bridge_unit set status = 0 where unit_id = '%s';" % (unit_id))
        data = {
            "status": 1,
            "message": u"删除评分单元成功",
            "data": []
        }
    except Exception as e:
        data = {
            "status": 0,
            "message": u"删除评分单元失败",
            "data": []
        }
    return JsonResponse(data)


# 功能：选择构件编号页面
# 传入参数：request
# 返回参数：response
@auth
def QLXJ_QLLB_PFDY_number(request):
    bridge_id = request.GET["bridge_id"]
    unit_id = request.GET["unit_id"]
    datalist = []
    try:
        row = MySQLDB.SelectMySql("select count(1) from t_bridge_unit_component where unit_id ='%s';" % (unit_id))
        if row[0][0] == 0:
            try:
                # todo 未完成修改，应修改为根据其自身桥梁类型存入桥梁部件信息
                row1 = Common.GetPartsListByBridge(bridge_id)
                # row1 = MySQLDB.SelectMySql(
                #     "select parts_code,bridge_parts_name,position_code,weight as default_weight,weight from t_sys_bridge_parts where param_bridge_type_code = 'bridgeType-0';")
                if len(row1) > 0:
                    # key = ["bridge_parts_id","bridge_parts_name","parts_code","weight","parts_no","position_code"]
                    # key = ["parts_code", "component_name", "position_code", "default_weight", "weight"]
                    for i in range(len(row1)):
                        insertCmd = "INSERT INTO `t_bridge_unit_component`(`unit_id`,`bridge_id`,`parts_code`,`component_name`,`position_code`,`default_weight`,`weight`,`status`) select '%s','%s',parts_code,bridge_parts_name,position_code,weight as default_weight,weight,1 from t_sys_bridge_parts where param_bridge_type_code = '%s' and parts_code = '%s';" % (unit_id, bridge_id, row1[i][4], row1[i][1])
                        row5 = MySQLDB.CommitMySql(insertCmd)
            except Exception as e:
                pass
    except Exception as e:
                pass
    try:
        row2 = MySQLDB.SelectMySql(
                    "select unit_component_id,unit_id,bridge_id,parts_code,component_name,position_code,default_weight,weight from t_bridge_unit_component where unit_id = '%s' and status=1;" % unit_id)
        if len(row2) > 0:
            key = ["unit_component_id","unit_id","bridge_id","parts_code", "component_name", "position_code", "default_weight", "weight"]
            for i in range(len(row2)):
                info = {}
                for j in range(len(key)):
                    info[key[j]] = row2[i][j]
                    if key[j] == "parts_code":
                        row4 = MySQLDB.SelectMySql(
                                    "select count(1) from t_bridge_member where bridge_id='%s' and parts_code='%s';" % (
                                    bridge_id, info[key[j]]))
                        if row4[0][0] == 0:
                            info["status"] = 0
                        else:
                            info["status"] = 1
                datalist.append(info)
    except Exception as e:
        datalist = []
    return render(request, "QLGL/QLSJ/QLGL_QLSJ_QLLB_PFDY_CHOOSE.html", {"datalist": datalist})
# 功能：评分单元权重保存
# 传入参数：request
# 返回参数：response
@authjson
def QLXJ_QLLB_PFDY_weight(request):
    unit_id = request.GET["unit_id"]
    bridge_id = request.GET["bridge_id"]
    parts_code = request.GET["parts_code"]
    component_name = request.GET["component_name"]
    position_code = request.GET["position_code"]
    default_weight = request.GET["default_weight"]
    weight = request.GET["weight"]
    try:
        row2 = MySQLDB.CommitMySql("UPDATE `t_bridge_unit_component` SET `weight`= '%s' where unit_id ='%s' and bridge_id ='%s' and parts_code ='%s' and component_name ='%s' and position_code ='%s' and default_weight ='%s'and status=1;"% (weight,unit_id,bridge_id,parts_code,component_name,position_code,default_weight))
        data = {
                "status":1,
                "message":u"权重保存成功",
                "data":[]
                 }
    except Exception as e:
        data = {
                "status":0,
                "message":u"权重保存失败",
                "data":[]
                 }
    return JsonResponse(data)
# 功能：评分单元权重删除
# 传入参数：request
# 返回参数：response
@authjson
def QLXJ_QLLB_PFDY_weight_del(request):
    unit_id = request.GET["unit_id"]
    bridge_id = request.GET["bridge_id"]
    parts_code = request.GET["parts_code"]
    component_name = request.GET["component_name"]
    position_code = request.GET["position_code"]
    default_weight = request.GET["default_weight"]
    weight = request.GET["weight"]
    try:
        row2 = MySQLDB.CommitMySql("UPDATE `t_bridge_unit_component` SET status = 0 where `weight`= '%s'and unit_id ='%s' and bridge_id ='%s' and parts_code ='%s' and component_name ='%s' and position_code ='%s' and default_weight ='%s';"% (weight,unit_id,bridge_id,parts_code,component_name,position_code,default_weight))
        data = {
                "status":1,
                "message":u"权重删除成功",
                "data":[]
                 }
    except Exception as e:
        data = {
                "status":0,
                "message":u"权重删除失败",
                "data":[]
                 }
    return JsonResponse(data)
# 功能：选择构件页面
# 传入参数：request
# 返回参数：response
@auth
def QLXJ_QLLB_PFDY_SelectComponent(request):
    bridge_id = request.GET["bridge_id"]
    parts_code = request.GET["parts_code"]
    unit_component_id = request.GET["unit_component_id"]
    unit_id = request.GET["unit_id"]
    return render(request, "QLGL/QLSJ/QLGL_QLSJ_QLLB_PFDY_XZ.html",
                  {"bridge_id": json.dumps(bridge_id), "unit_id": json.dumps(unit_id),"parts_code": json.dumps(parts_code),"unit_component_id": json.dumps(unit_component_id)})
# 功能：选择构件数据
# 传入参数：request
# 返回参数：response
@authjson
def QLXJ_QLLB_PFDY_SelectComponent_data(request):
    bridge_id = request.GET["bridge_id"]
    parts_code = request.GET["parts_code"]
    unit_component_id = request.GET["unit_component_id"]
    unit_id = request.GET["unit_id"]
    datalist = []
    try:
        # 获取构件信息
        row = MySQLDB.SelectMySql(
            "SELECT bridge_member_id,(SELECT parts_name FROM `t_sys_parts` WHERE parts_code='%s' and status = 1) as parts_name,member_name,notes from t_bridge_member where bridge_id='%s' and parts_code = '%s'  and status = 1;" % (
            parts_code,bridge_id,parts_code))
        if len(row) > 0:
            key = ["bridge_member_id","parts_name", "member_name", "notes"]
            for i in range(len(row)):
                info = {}
                for j in range(len(key)):
                    info[key[j]] = row[i][j]
                    if key[j]== "bridge_member_id":
                        row1 = MySQLDB.SelectMySql("SELECT count(1) from t_bridge_unit_member where unit_id = '%s' and member_id = '%s'  " % (
            unit_id,row[i][j]))
                        if row1[0][0]==0:
                            info["flag"] = 0
                        else:
                            info["flag"] = 1
                datalist.append(info)
        data = {
            "status": 1,
            "message": u"读取构件信息成功",
            "data": datalist
            }
    except Exception as e:
        data = {
            "status": 0,
            "message": u"读取构件信息失败",
            "data": []
            }
    return JsonResponse(data)
# 功能：选择构件保存
# 传入参数：request
# 返回参数：response
@authjson
def QLXJ_QLLB_PFDY_SelectComponent_save(request):
    bridge_id = request.GET["bridge_id"]
    parts_code = request.GET["parts_code"]
    unit_component_id = request.GET["unit_component_id"]
    unit_id = request.GET["unit_id"]
    bridge_member_id = request.GET["bridge_member_id"]
    # member_name = request.GET["member_name"]
    datalist = []
    try:
        MySQLDB.CommitMySql("DELETE FROM t_bridge_unit_member where unit_id = '%s'and component_id = '%s';" % (unit_id,unit_component_id))
        temp = bridge_member_id.split('|')
        for t in temp:
            if len(t) > 0:
                row = MySQLDB.SelectMySql(
            "SELECT member_name,notes from t_bridge_member where bridge_member_id = '%s';" % t)
                row2 = MySQLDB.SelectMySql("SELECT component_name FROM `t_bridge_unit_component`where unit_id = '%s' and unit_component_id = '%s'  " % (
            unit_id,unit_component_id))
                row3 = MySQLDB.CommitMySql(
                        "INSERT INTO `t_bridge_unit_member`(`unit_id`,`parts_code`,`component_id`,`component_name`,`member_id`,`member_name`,`member_notes`)VALUES ('%s','%s','%s','%s','%s','%s','%s')" % (unit_id, parts_code,unit_component_id,row2[0][0],t,row[0][0],row[0][1]))
        data = {
        "status": 1,
        "message": u"选择构件保存成功",
        "data": []
        }
    except Exception as e:
        data = {
            "status": 0,
            "message": u"选择构件保存失败",
            "data": []
            }
    return JsonResponse(data)
# 功能：已选择构件页面
# 传入参数：request
# 返回参数：response
@auth
def QLXJ_QLLB_PFDY_SelectedComponent_index(request):
    bridge_id = request.GET["bridge_id"]
    parts_code = request.GET["parts_code"]
    unit_component_id = request.GET["unit_component_id"]
    unit_id = request.GET["unit_id"]
    return render(request, "QLGL/QLSJ/QLGL_QLSJ_QLLB_PFDY_YX.html",
                  {"bridge_id": json.dumps(bridge_id), "unit_id": json.dumps(unit_id),"parts_code": json.dumps(parts_code),"unit_component_id": json.dumps(unit_component_id)})  
# 功能：已选择构件数据
# 传入参数：request
# 返回参数：response
@authjson
def QLXJ_QLLB_PFDY_SelectedComponent_data(request):
    bridge_id = request.GET["bridge_id"]
    parts_code = request.GET["parts_code"]
    unit_component_id = request.GET["unit_component_id"]
    unit_id = request.GET["unit_id"]
    SelectComponent = []
    try:
        #已选择构件
        row = MySQLDB.SelectMySql(
            "SELECT (SELECT parts_name FROM `t_sys_parts` WHERE parts_code='%s' and status = 1) as parts_name,a.unit_member_id,a.member_id,a.member_name,a.member_notes as notes from t_bridge_unit_member a inner join t_bridge_member b on a.member_id = b.bridge_member_id and b.status = 1 where a.unit_id='%s' and a.component_id = '%s';" % (
            parts_code,unit_id,unit_component_id))
        if len(row) > 0:
            key = ["parts_name","unit_member_id", "member_id", "member_name", "notes"]
            for i in range(len(row)):
                info = {}
                for j in range(len(key)):
                    info[key[j]] = row[i][j]
                SelectComponent.append(info)
        data = {
        "status": 1,
        "message": u"读取已选构件信息成功",
        "data": SelectComponent
        }
    except Exception as e:
        data = {
            "status": 0,
            "message": u"读取已选构件信息失败",
            "data": []
                }
    return JsonResponse(data)

# 功能：正立面照
# 传入参数：request
# 返回参数：response
@auth
def QLXJ_QLLB_photo(request):
    bridge_id = request.GET["bridge_id"]
    datalist = []
    try:
        # 正立面照
        row = MySQLDB.SelectMySql(
            "SELECT bridge_id,file_id,type,url,local,file_name from t_bridge_file where status = 1 and bridge_id = '%s';" % (
            bridge_id))
        if len(row) > 0:
            key = ["bridge_id", "file_id", "type", "url", "local", "file_name"]
            for i in range(len(row)):
                info = {}
                for j in range(len(key)):
                    info[key[j]] = row[i][j]
                datalist.append(info)
    except Exception as e:
        pass
    return render(request, "QLGL/QLSJ/QLGL_QLSJ_QLLB_ZLMZ.html", {"datalist": datalist})


# 功能：桥梁详情
# 传入参数：request
# 返回参数：response
@auth
def QLXJ_QLLB_detail(request):
    bridge_id = request.GET["bridge_id"]
    datalist = []
    try:
        row1 = MySQLDB.SelectMySql(
            "SELECT bridge_id,`bridge_code`,`bridge_name`,`line_id`,(select line_no from t_sys_line where line_id = a.`line_id` and status = 1) as line_no,`line_name`,`section_id`,`section_name`,`pile_no`,`maintenance_depart`,(select param_name from t_sys_param where param_id = `param_deck_pavement`) as param_deck_pavement,`length`,`under_height`,`angle`,(select param_name from t_sys_param where param_id = `param_left_facilities`) as param_left_facilities,(select param_name from t_sys_param where param_id = `param_right_facilities`) as param_right_facilities,`span_total`,(select param_name from t_sys_param where param_id = `param_pier_material`) as param_pier_material,(select param_name from t_sys_param where param_id = `param_pier_style`) as param_pier_style,(select param_name from t_sys_param where param_id = `param_pier_base_form`) as param_pier_base_form,(select param_name from t_sys_param where param_id = `param_abutment_material`) as param_abutment_material,(select param_name from t_sys_param where param_id = `param_abutment_style`) as param_abutment_style,(select param_name from t_sys_param where param_id = `param_abutment_base_form`) as param_abutment_base_form,(select param_name from t_sys_param where param_id = `param_expansion_type`) as param_expansion_type,`expansion_position`,(select param_name from t_sys_param where param_id = `param_support_type`) as param_support_type,`remark`,`status`,date_format(build_date,'%%Y-%%m-%%d') as build_date ,(select param_name from t_sys_param where param_id = `param_line_level`) as param_line_level,`left_facilities_width`,`right_facilities_width`,`central_isolation_belt`,`driveway_width`,`approach_width`,`approach_road_width`,case approach_linear when 1 then '直线' when 2 then '曲线' else '' end as approach_linear,`under_cross_road`,`under_cross_road_no`,(select param_name from t_sys_param where param_id = `param_func_type`) as param_func_type,(select param_name from t_sys_param where param_id = `param_design_load`) as param_design_load,`slope_angle`,`build_depart`,`design_depart`,`construction_depart`,`longitude`,`latitude`,`run_load`,`bridge_total_width`,(select param_name from t_sys_param where param_id = `param_bridge_category`) as param_bridge_category,bridge_longitudinal_slope, bridge_cross_slope, deck_elevation, (select param_name from t_sys_param where param_id = `param_tower_structure_form`) as param_tower_structure_form, (select param_name from t_sys_param where param_id = `param_tower_material`) as param_tower_material, bridge_clear_height, earthquake_pac, abutment_slope_protection, pier_body, modulation_structure, water_level, design_water_level, historical_flood_level, (select param_name from t_sys_param where param_id = `param_tower_basis_material`) as param_tower_basis_material, (select param_name from t_sys_param where param_id = `param_tower_base_form`) as param_tower_base_form from t_bridge_base a where bridge_id='%s';" % bridge_id)
        if len(row1) > 0:
            key = ['bridge_id', 'bridge_code', 'bridge_name', 'line_id', 'line_no', 'line_name', 'section_id', 'section_name', 'pile_no', 'maintenance_depart', 'param_deck_pavement', 'length', 'under_height', 'angle', 'param_left_facilities', 'param_right_facilities', 'span_total', 'param_pier_material', 'param_pier_style', 'param_pier_base_form', 'param_abutment_material', 'param_abutment_style', 'param_abutment_base_form', 'param_expansion_type', 'expansion_position', 'param_support_type', 'remark', 'status', 'build_date', 'param_line_level', 'left_facilities_width', 'right_facilities_width', 'central_isolation_belt', 'driveway_width', 'approach_width', 'approach_road_width', 'approach_linear', 'under_cross_road', 'under_cross_road_no', 'param_func_type', 'param_design_load', 'slope_angle', 'build_depart', 'design_depart', 'construction_depart', 'longitude', 'latitude', 'run_load', 'bridge_total_width', 'param_bridge_category', 'bridge_longitudinal_slope', 'bridge_cross_slope', 'deck_elevation', 'param_tower_structure_form', 'param_tower_material', 'bridge_clear_height', 'earthquake_pac', 'abutment_slope_protection', 'pier_body', 'modulation_structure', 'water_level', 'design_water_level', 'historical_flood_level', 'param_tower_basis_material', 'param_tower_base_form']
            key_int = ["length", "under_height", "angle", "left_facilities_width",
                       "right_facilities_width", "central_isolation_belt", "driveway_width", "approach_width",
                       "approach_road_width"]
            for i in range(len(row1)):
                info = {}
                for j in range(len(key)):
                    if key[j] in key_int:
                        info[key[j]] = str(row1[i][j])
                    elif row1[i][j] == None:
                        info[key[j]] = ""
                    else:
                        info[key[j]] = row1[i][j]

                datalist.append(info)
    except Exception as e:
        datalist = []
    uplist = []
    try:
        # 获取上部结构信息
        row = MySQLDB.SelectMySql(
            "select upperpart_id,upper_order,span_number,span_arrangement,(select param_name from t_sys_param where param_id = param_deck_board_position) param_deck_board_position,(select param_name from t_sys_param where param_id = param_force_form) param_force_form,(select param_name from t_sys_param where param_id = param_form) param_form,(select param_name from t_sys_param where param_id = param_material) param_material,(select param_name from t_sys_param where param_id = param_bridge_type) param_bridge_type ,param_bridge_type as param_bridge_type_id  from t_bridge_upperpart where bridge_id = %s;" % bridge_id)
        if len(row) > 0:
            key = ["upperpart_id", "upper_order", "span_number", "span_arrangement", "param_deck_board_position",
                   "param_force_form", "param_form", "param_material", "param_bridge_type","param_bridge_type_id"]
            for i in range(len(row)):
                info = {}
                for j in range(len(key)):
                    info[key[j]] = row[i][j]
                uplist.append(info)
    except Exception as e:
        uplist = []
    return render(request, "QLGL/QLSJ/QLGL_QLSJ_QLLB_XQ.html", {"datalist": datalist,"uplist": uplist})


# 功能：删除某个桥梁信息
# 传入参数：request
# 返回参数：response
@authjson
def QLXJ_QLLB_DEL(request):
    bridge_id = request.GET["bridge_id"]
    datalist = []
    try:
        # 删除桥梁信息
        row = MySQLDB.CommitMySql("UPDATE t_bridge_base set status=0 where bridge_id='%s';" % (bridge_id))
        data = {
            "status": 1,
            "message": u"del桥梁信息信息成功",
            "data": []
        }
    except Exception as e:
        data = {
            "status": 0,
            "message": u"del桥梁信息信息信息失败",
            "data": []
        }
    return JsonResponse(data)

# 功能：设置子部件页面
# 传入参数：request
# 返回参数：response
@authjson
def QLXJ_QLLB_ZBJGZ(request):
    bridge_type = request.GET.get("bridge_type", "")
    parts_code = request.GET.get("parts_code", "")
    bridgeTypeName = ""
    partsName = ""
    try:
        # 查询桥型、部件名
        row = MySQLDB.SelectMySql("select a.param_name,b.parts_name from t_sys_param a ,t_sys_parts b where a.param_code = '%s' and b.parts_code = '%s' and a.status = 1 and b.status = 1;" % (bridge_type, parts_code))
        if len(row) > 0:
            bridgeTypeName = row[0][0]
            partsName = row[0][1]
    except Exception as e:
        pass
    return render(request, "QLGL/ZDSJ/QLGL_ZDSJ_BJQZ_SUB.html", {"bridge_type":json.dumps(bridge_type), "parts_code": json.dumps(parts_code), "bridgeTypeName": bridgeTypeName, "partsName":partsName})

# 功能：查询子部件列表
# 传入参数：request
# 返回参数：response
@auth
def QLXJ_QLLB_ZBJGZ_LIST(request):
    bridge_type = request.GET.get("bridge_type", "")
    parts_code = request.GET.get("parts_code", "")
    subPartList = []
    try:
        # 查询子部件信息
        row = MySQLDB.SelectMySql("select parts_sub_id,position_code,name from t_sys_parts_sub where param_bridge_type_code = '%s' and parts_code = '%s' and status = 1;" % (bridge_type, parts_code))
        if len(row) > 0:
            for i in range(len(row)):
                info = {}
                info["parts_sub_id"] = row[i][0]
                info["position_code"] = row[i][1]
                info["name"] = row[i][2]
                subPartList.append(info)
        data = {
            "status": 1,
            "message": u"查询成功",
            "data": subPartList
        }
    except Exception as e:
        data = {
            "status": 0,
            "message": e.message,
            "data": []
        }
    return JsonResponse(data)

# 功能：新增子部件页面
# 传入参数：request
# 返回参数：response
@auth
def QLXJ_QLLB_ZBJGZ_ADD(request):
    bridge_type = request.GET.get("bridge_type", "")
    parts_code = request.GET.get("parts_code", "")
    return render(request, "QLGL/ZDSJ/QLGL_ZDSJ_BJQZ_SUB_ADD.html", {"bridge_type":json.dumps(bridge_type), "parts_code": json.dumps(parts_code)})

# 功能：新增子部件保存
# 传入参数：request
# 返回参数：response
@authjson
def QLXJ_QLLB_ZBJGZ_ADDSAVE(request):
    bridge_type = request.POST.get("bridge_type", "")
    parts_code = request.POST.get("parts_code", "")
    subPartName = request.POST.get("subpart_name", "")
    try:
        # 验证子部件名称，不可重复
        selCmd = "select 1 from t_sys_parts_sub where param_bridge_type_code = '%s' and parts_code = '%s' and status = 1 and name = '%s';" % (bridge_type, parts_code, subPartName)
        row = MySQLDB.SelectMySql(selCmd)
        if len(row) > 1:
            raise Exception(u"桥型部件下已存在此子部件名称，请重新填写")
        # 新增子部件信息
        insertCmd = "insert into t_sys_parts_sub (param_bridge_type_code,parts_code,position_code,name) select '%s',parts_code,position_code,'%s' from t_sys_parts where parts_code = '%s' and status = 1;" % (bridge_type, subPartName, parts_code)
        row = MySQLDB.CommitMySql(insertCmd)
        if row != 'ok' > 0:
            raise Exception(u"新增子部件失败")
        data = {
            "status": 1,
            "message": u"新增成功",
            "data": []
        }
    except Exception as e:
        data = {
            "status": 0,
            "message": e.message,
            "data": []
        }
    return JsonResponse(data)
# 功能：修改子部件页面
# 传入参数：request
# 返回参数：response
@auth
def QLXJ_QLLB_ZBJGZ_MOD(request):
    parts_sub_id = request.GET.get("parts_sub_id", -1)
    sub_parts_name = ""
    try:
        selCmd = "select name from t_sys_parts_sub where parts_sub_id = '%s';" % parts_sub_id
        row = MySQLDB.SelectMySql(selCmd)
        if len(row):
            sub_parts_name = row[0][0]
    except Exception as e:
        pass
    return render(request, "QLGL/ZDSJ/QLGL_ZDSJ_BJQZ_SUB_MODIFY.html", {"parts_sub_id": parts_sub_id, "sub_parts_name": sub_parts_name})

# 功能：修改子部件保存
# 传入参数：request
# 返回参数：response
@authjson
def QLXJ_QLLB_ZBJGZ_MODSAVE(request):
    parts_sub_id = request.POST.get("parts_sub_id", -1)
    subPartName = request.POST.get("sub_parts_name", "")
    try:
        # 验证子部件名称，不可重复
        selCmd = "select 1 from t_sys_parts_sub a inner join t_sys_parts_sub b on a.param_bridge_type_code = b.param_bridge_type_code and a.parts_code = b.parts_code where b.parts_sub_id = '%s' and a.name = '%s';" % (parts_sub_id, subPartName)
        row = MySQLDB.SelectMySql(selCmd)
        if len(row) > 1:
            raise Exception(u"桥型部件下已存在此子部件名称，请重新填写")
        # 新增子部件信息
        updateCmd = "update t_sys_parts_sub set update_date = now(),name = '%s' where parts_sub_id = '%s';" % (subPartName, parts_sub_id )
        row = MySQLDB.CommitMySql(updateCmd)
        if row != 'ok' > 0:
            raise Exception(u"修改子部件失败")
        data = {
            "status": 1,
            "message": u"修改子部件成功",
            "data": []
        }
    except Exception as e:
        data = {
            "status": 0,
            "message": e.message,
            "data": []
        }
    return JsonResponse(data)
# 功能：删除子部件
# 传入参数：request
# 返回参数：response
@authjson
def QLXJ_QLLB_ZBJGZ_DELSAVE(request):
    parts_sub_id = request.POST.get("parts_sub_id", -1)
    try:
        # 删除子部件信息
        updateCmd = "update t_sys_parts_sub set update_date = now(),status = 0 where parts_sub_id = '%s';" % (parts_sub_id)
        row = MySQLDB.CommitMySql(updateCmd)
        if row != 'ok' > 0:
            raise Exception(u"删除失败")
        data = {
            "status": 1,
            "message": u"删除成功",
            "data": []
        }
    except Exception as e:
        data = {
            "status": 0,
            "message": e.message,
            "data": []
        }
    return JsonResponse(data)

# 功能：查看子部件规则页面
# 传入参数：request
# 返回参数：response
@authjson
def QLXJ_QLLB_ZBJGZ_RULE(request):
    parts_sub_id = request.GET.get("parts_sub_id", -1)
    positionInfo = {"1": "左右幅","2": "左右侧","3": "无左右幅"}
    subPartInfo = {}
    subPartRuleList = []
    try:
        # 显示子部件信息
        row = MySQLDB.SelectMySql("select parts_sub_id,param_bridge_type_code,parts_code,position_code,name from t_sys_parts_sub where parts_sub_id = '%s';" % (parts_sub_id))
        if len(row) > 0:
            subPartInfo["parts_sub_id"] = row[0][0]
            subPartInfo["parts_sub_name"] = row[0][4]
            subPartInfo["param_bridge_type_code"] = row[0][1]
            subPartInfo["parts_code"] = row[0][2]
            subPartInfo["position_code"] = row[0][3]
        # 子部件规则信息
        selCmd = "select parts_sub_rule_id,parts_sub_id,param_num,parts_num,parts_note,position,ifnull(remark,'') remark from t_sys_parts_sub_rule where parts_sub_id = '%s' and status = 1;" % parts_sub_id
        row = MySQLDB.SelectMySql(selCmd)
        keys = ['parts_sub_rule_id', 'parts_sub_id', 'param_num', 'parts_num', 'parts_note', 'position', 'remark']
        if len(row) > 0:
            subPartRuleList = [dict(zip(keys, w)) for w in row]
    except Exception as e:
        pass
    return render(request, "QLGL/ZDSJ/QLGL_ZDSJ_BJQZ_SUB_RULE.html", {"parts_sub_id":parts_sub_id,"subPartInfo": subPartInfo, "subPartRuleList": subPartRuleList,"positionInfo":positionInfo})

def QLXJ_QLLB_ZBJGZ_RULE_LIST(request):
    parts_sub_id = request.POST.get("parts_sub_id", -1)
    return render(request,"",{})

# 功能：子部件规则新增页面
# 传入参数：request
# 返回参数：response
@auth
def QLXJ_QLLB_ZBJGZ_RULE_ADD(request):
    parts_sub_id = request.GET.get("parts_sub_id", -1)
    sub_parts_name = ""
    positionInfo = [{"id": 1, "value": "左右幅"},{"id": 2, "value": "左右侧"},{"id": 3, "value": "无左右幅"}]
    try:
        selCmd = "select name from t_sys_parts_sub where parts_sub_id = '%s';" % parts_sub_id
        row = MySQLDB.SelectMySql(selCmd)
        if len(row):
            sub_parts_name = row[0][0]
    except Exception as e:
        pass
    return render(request, "QLGL/ZDSJ/QLGL_ZDSJ_BJQZ_SUB_RULE_ADD.html", {"parts_sub_id": parts_sub_id, "sub_parts_name": sub_parts_name, "positionInfo": positionInfo})

# 功能：子部件规则新增保存
# 传入参数：request
# 返回参数：response
@authjson
def QLXJ_QLLB_ZBJGZ_RULE_ADDSAVE(request):
    parts_sub_id = request.POST.get("parts_sub_id", -1)
    param_num = request.POST.get("param_num", 0)
    parts_num = request.POST.get("parts_num", "")
    parts_note = request.POST.get("parts_note", "")
    position = request.POST.get("position", "")
    remark = request.POST.get("remark", "")
    try:
        # 验证子部件规则，同一参数数量，不可重复
        selCmd = "select 1 from t_sys_parts_sub_rule where parts_sub_id = '%s' and param_num = '%s';" % (parts_sub_id, param_num)
        row = MySQLDB.SelectMySql(selCmd)
        if len(row) > 1:
            raise Exception(u"子部件已存在%s参数规则，请重新填写"%param_num)
        # 新增子部件信息
        insertCmd = "insert into t_sys_parts_sub_rule(parts_sub_id,param_num,parts_num,parts_note,position,remark) select '%s','%s','%s','%s','%s','%s'" % (parts_sub_id, param_num, parts_num, parts_note, position, remark)
        row = MySQLDB.CommitMySql(insertCmd)
        if row != 'ok' > 0:
            raise Exception(u"新增规则失败")
        data = {
            "status": 1,
            "message": u"新增规则成功",
            "data": []
        }
    except Exception as e:
        data = {
            "status": 0,
            "message": e.message,
            "data": []
        }
    return JsonResponse(data)

# 功能：子部件规则修改页面
# 传入参数：request
# 返回参数：response
@auth
def QLXJ_QLLB_ZBJGZ_RULE_MOD(request):
    parts_sub_rule_id = request.GET.get("parts_sub_rule_id", -1)
    sub_parts_name = ""
    ruleInfo = {}
    positionInfo = [{"id": 1, "value": "左右幅"},{"id": 2, "value": "左右侧"},{"id": 3, "value": "无左右幅"}]
    try:
        selCmd = "select b.parts_sub_rule_id,b.parts_sub_id,b.param_num,b.parts_num,b.parts_note,b.position,b.remark,a.name from t_sys_parts_sub a inner join t_sys_parts_sub_rule b on a.parts_sub_id= b.parts_sub_id where b.parts_sub_rule_id = '%s' and b.status = 1;" % parts_sub_rule_id
        row = MySQLDB.SelectMySql(selCmd)
        if len(row):
            ruleInfo["parts_sub_rule_id"] = row[0][0]
            ruleInfo["parts_sub_id"] = row[0][1]
            ruleInfo["param_num"] = row[0][2]
            ruleInfo["parts_num"] = row[0][3]
            ruleInfo["parts_note"] = row[0][4]
            ruleInfo["position"] = row[0][5]
            ruleInfo["remark"] = row[0][6]
            sub_parts_name = row[0][7]
    except Exception as e:
        pass
    return render(request, "QLGL/ZDSJ/QLGL_ZDSJ_BJQZ_SUB_RULE_MODIFY.html", {"parts_sub_rule_id": parts_sub_rule_id, "sub_parts_name": sub_parts_name, "positionInfo": positionInfo, "ruleInfo": ruleInfo})

# 功能：子部件规则修改保存
# 传入参数：request
# 返回参数：response
@authjson
def QLXJ_QLLB_ZBJGZ_RULE_MODSAVE(request):
    parts_sub_rule_id = request.POST.get("parts_sub_rule_id", -1)
    param_num = request.POST.get("param_num", 0)
    parts_num = request.POST.get("parts_num", "")
    parts_note = request.POST.get("parts_note", "")
    position = request.POST.get("position", "")
    remark = request.POST.get("remark", "")
    try:
        # 验证子部件规则，同一参数数量，不可重复
        selCmd = "select 1 from t_sys_parts_sub_rule where parts_sub_rule_id = '%s' and param_num = '%s' and status = 1;" % (parts_sub_rule_id, param_num)
        row = MySQLDB.SelectMySql(selCmd)
        if len(row) > 1:
            raise Exception(u"子部件已存在%s参数规则，请重新填写"%param_num)
        # 新增子部件信息
        updateCmd = "update t_sys_parts_sub_rule set update_date = now(),param_num = '%s',parts_num= '%s',parts_note= '%s',position= '%s',remark= '%s' where parts_sub_rule_id = '%s';" % (param_num, parts_num, parts_note, position, remark, parts_sub_rule_id)
        row = MySQLDB.CommitMySql(updateCmd)
        if row != 'ok' > 0:
            raise Exception(u"修改规则失败")
        data = {
            "status": 1,
            "message": u"修改规则成功",
            "data": []
        }
    except Exception as e:
        data = {
            "status": 0,
            "message": e.message,
            "data": []
        }
    return JsonResponse(data)
# 功能：子部件规则删除
# 传入参数：request
# 返回参数：response
@authjson
def QLXJ_QLLB_ZBJGZ_RULE_DELSAVE(request):
    parts_sub_rule_id = request.POST.get("parts_sub_rule_id", -1)
    try:
        # 删除子部件规则信息
        updateCmd = "update t_sys_parts_sub_rule set update_date = now(),status=0 where parts_sub_rule_id = '%s';" % (parts_sub_rule_id)
        row = MySQLDB.CommitMySql(updateCmd)
        if row != 'ok' > 0:
            raise Exception(u"修改规则失败")
        data = {
            "status": 1,
            "message": u"修改规则成功",
            "data": []
        }
    except Exception as e:
        data = {
            "status": 0,
            "message": e.message,
            "data": []
        }
    return JsonResponse(data)

